Skip to content

Commit

Permalink
feat: add google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonet authored and vsavkin committed Aug 8, 2018
1 parent 2a8fa9e commit 72dbaea
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 65 deletions.
3 changes: 2 additions & 1 deletion apps/angular-console-e2e/cypress.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"video": true,
"videosFolder": "../../dist/apps/angular-console-e2e/video",
"screenshotsFolder": "../../dist/apps/angular-console-e2e/screenshot",
"projectId": "x2ebye"
"projectId": "x2ebye",
"chromeWebSecurity": false
}
10 changes: 6 additions & 4 deletions apps/angular-console/src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<div fxLayout="column" fxFlex>
<ui-contextual-action-bar></ui-contextual-action-bar>
<ui-contextual-action-bar></ui-contextual-action-bar>

<div fxFlex class="outlet-container" [@routerTransition]="routerTransition | async">
<router-outlet></router-outlet>
</div>
<div fxFlex class="outlet-container" [@routerTransition]="routerTransition | async">
<router-outlet></router-outlet>
</div>

<ui-data-collection></ui-data-collection>
</div>
17 changes: 14 additions & 3 deletions apps/angular-console/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import {
workspaceRoutes
} from '@angular-console/feature-workspaces';
import { UiModule } from '@angular-console/ui';
import { CancelCommandGuard, Messenger } from '@angular-console/utils';
import {
AnalyticsCollector,
CancelCommandGuard,
Messenger
} from '@angular-console/utils';
import { Apollo, ApolloModule } from 'apollo-angular';
import { HttpLink, HttpLinkModule } from 'apollo-angular-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
Expand Down Expand Up @@ -45,27 +49,34 @@ import { AppComponent } from './app.component';
{ paramsInheritanceStrategy: 'always' }
)
],
providers: [CancelCommandGuard],
providers: [CancelCommandGuard, AnalyticsCollector],
bootstrap: [AppComponent]
})
export class AppModule {
constructor(
analytics: AnalyticsCollector,
apollo: Apollo,
httpLink: HttpLink,
matIconRegistry: MatIconRegistry,
messenger: Messenger
) {
analytics.setUpRouterLogging();

const errorLink = onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.forEach(({ message }) => {
messenger.error(message);
analytics.reportException(message);
});
} else if (networkError) {
const n: any = networkError;
if (n.error && n.error.errors && n.error.errors.length > 0) {
messenger.error(n.error.errors[0].message);
const message = n.error.errors[0].message;
messenger.error(message);
analytics.reportException(message);
} else {
messenger.error(n.message);
analytics.reportException(n.message);
}
}
});
Expand Down
17 changes: 10 additions & 7 deletions electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
"main": "electron.js",
"license": "MIT",
"dependencies": {
"@angular/cli": "6.1.1",
"@ngrx/schematics": "6.1.0",
"@nrwl/schematics": "6.2.0",
"drivelist": "6.3.0",
"electron-updater": "^2.21.4",
"express": "^4.16.3",
"express-graphql": "^0.6.12",
"graphql": "0.13.2",
"resolve": "1.8.1",
"fix-path": "2.1.0",
"get-port": "3.2.0",
"@angular/cli": "6.1.1",
"@nrwl/schematics": "6.2.0",
"@ngrx/schematics": "6.1.0",
"graphql": "0.13.2",
"node-pty-prebuilt": "0.7.3",
"electron-updater": "^2.21.4",
"drivelist": "6.3.0"
"resolve": "1.8.1",
"universal-analytics": "^0.4.17",
"uuid": "^3.3.2",
"electron-store": "2.0.0"
},
"devDependencies": {
"electron": "^2.0.3"
Expand Down
68 changes: 68 additions & 0 deletions electron/src/analytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import * as ua from 'universal-analytics';
import {ipcMain} from 'electron';
const uuidv4 = require('uuid/v4');
const Store = require('electron-store');
const store = new Store();

const visitor = new ua.Visitor('UA-88380372-8', getUuiId(), {
https: true
});

// must be called before electron app is created
export function setUpAnalytics() {
process.env.trackingID = 'UA-88380372-8';
ipcMain.on(
'reportEvent',
(event: any, arg: any) => reportEvent(arg.categroy, arg.action, arg.label, arg.value));
ipcMain.on(
'dataCollectionEvent',
(event: any, arg: any) => dataCollectionEvent(arg.value));
ipcMain.on(
'reportPageView',
(event: any, arg: any) => reportPageView(arg.path));
ipcMain.on(
'reportException',
(event: any, arg: any) => reportException(arg.description));
}

export function dataCollectionEvent(value: boolean) {
store.set('canCollectData', value);
visitor.event("DataCollection", "DataCollectionResponse", value.toString()).send();
}

export function reportEvent(category: string, action: string, label?: string, value?: number) {
if (canCollectData()) {
if (value) {
visitor.event(category, action, label, value, {}).send();
} else {
visitor.event(category, action, label).send();
}
}
}

export function reportException(description: string) {
if (canCollectData()) {
console.error(description);
visitor.exception(description).send();
}
}

export function reportPageView(path: string) {
if (canCollectData()) {
visitor.pageview(path, 'Angular Console', '6.0.0-beta.1').send();
}
}

function getUuiId() {
if (store.get('uuid')) {
return store.get('uuid');
}
const uuid = uuidv4();
store.set('uuid', uuid);
return uuid;
}

function canCollectData(): boolean {
return store.get('canCollectData') === true;
}

57 changes: 35 additions & 22 deletions electron/src/electron.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { app, BrowserWindow, Menu, dialog } from 'electron';
import {app, BrowserWindow, dialog, Menu} from 'electron';
import * as path from 'path';
import { execSync, spawn } from 'child_process';
import { statSync, writeFileSync } from 'fs';
import {statSync} from 'fs';
import * as os from 'os';
import {autoUpdater} from 'electron-updater';
import {reportEvent, reportException, setUpAnalytics} from './analytics';

const fixPath = require('fix-path');
const getPort = require('get-port');
import * as os from 'os';
import { autoUpdater } from 'electron-updater';
import * as semver from 'semver';

let win: any;
let p: any;

fixPath();

const currentDirectory = process.cwd();

function createMenu() {
let menu = [];
const name = app.getName();
const common = [
const common: any = [
{
label: 'Edit',
submenu: [
Expand Down Expand Up @@ -63,7 +61,7 @@ function createMenu() {
label: 'Quit ' + name,
accelerator: 'Command+Q',
click() {
app.quit();
quit();
}
}
]
Expand All @@ -79,7 +77,7 @@ function createMenu() {
label: 'Exit',
accelerator: 'Ctrl+Q',
click() {
app.quit();
quit();
}
}
]
Expand All @@ -93,7 +91,7 @@ function createMenu() {
function createWindow() {
win = new BrowserWindow({ width: 800, height: 1400 });

getPort({ port: 7777 }).then(port => {
getPort({ port: 7777 }).then((port: number) => {
try {
startServer(port);
if (fileExists(path.join(currentDirectory, 'angular.json'))) {
Expand All @@ -103,22 +101,24 @@ function createWindow() {
}
} catch (e) {
showCloseDialog(`Error when starting Angular Console: ${e.message}`);
reportException(`Start failed: ${e.message}`);
}
});

win.on('close', () => {
win = null;
if (p) {
p.kill();
}
app.quit();
quit();
});
}

function startServer(port: number) {
console.log('starting server on port', port);
const {start} = require('./server/index');
start(port);
try {
const { start } = require('./server/index');
start(port);
} catch (e) {
reportException(`Start Server: ${e.message}`);
throw e;
}
}

function fileExists(filePath: string): boolean {
Expand All @@ -137,7 +137,7 @@ function showCloseDialog(message: string) {
};
dialog.showMessageBox(dialogOptions, i => {
if (i === 0) {
app.quit();
quit();
}
});
}
Expand All @@ -150,6 +150,7 @@ function showRestartDialog() {
};
dialog.showMessageBox(dialogOptions, i => {
if (i === 0) { // Restart
reportEvent('Lifecycle', 'QuitAndInstall');
autoUpdater.quitAndInstall();
}
});
Expand All @@ -167,14 +168,26 @@ function checkForUpdates() {
console.log('checkForUpdates is called. downloadPromise is null.');
}
} catch (e) {
console.log('checkForUpdates failed');
console.log(e.message);
reportException(e);
}
}
}, 0);
}

function startSession() {
reportEvent('Lifecycle', 'StartSession');
}

function quit() {
if (win) {
win = null;
app.quit();
}
}

app.on('ready', () => {
setUpAnalytics();
startSession();
createMenu();
createWindow();
checkForUpdates();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div id="data-collection" *ngIf="showMessage">
<p>
Help making Angular Console better by sending anonymous usage data to the Angular Console team.
</p>

<button mat-stroked-button color="primary" (click)="close(true)">Send</button>
<button mat-stroked-button (click)="close(false)">Do not send</button>
</div>
19 changes: 19 additions & 0 deletions libs/ui/src/lib/data-collection/data-collection.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#data-collection {
position: absolute;
width: 400px;
padding: 20px;
margin: 20px;
bottom: 0;
right: 0;
background-color: rgb(242, 242, 242);
border: 1px solid rgb(200, 200, 200);

p {
font-size: 14px;
color: rgba(0, 0, 0, 0.54) !important;
}

button {
margin-right: 5px;
}
}
23 changes: 23 additions & 0 deletions libs/ui/src/lib/data-collection/data-collection.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Component } from '@angular/core';
import { AnalyticsCollector, Settings } from '@angular-console/utils';

@Component({
selector: 'ui-data-collection',
templateUrl: './data-collection.component.html',
styleUrls: ['./data-collection.component.scss']
})
export class DataCollectionComponent {
constructor(
private readonly analytics: AnalyticsCollector,
private readonly settings: Settings
) {}

get showMessage() {
return this.settings.canCollectData() === undefined;
}

close(value: boolean) {
this.settings.setCanCollectData(value);
this.analytics.reportDataCollectionEvent(value);
}
}
7 changes: 5 additions & 2 deletions libs/ui/src/lib/ui.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
MatTreeModule
} from '@angular/material';
import { NormalizePathPipe } from './normalize-path.pipe';
import { DataCollectionComponent } from './data-collection/data-collection.component';

const IMPORTS = [
CdkTreeModule,
Expand Down Expand Up @@ -91,7 +92,8 @@ const IMPORTS = [
TaskSelectorComponent,
ContextualActionBarComponent,
DirectorySelectorComponent,
NormalizePathPipe
NormalizePathPipe,
DataCollectionComponent
],
providers: [ContextualActionBarService],
exports: [
Expand All @@ -102,7 +104,8 @@ const IMPORTS = [
TaskRunnerComponent,
TaskSelectorComponent,
TerminalComponent,
NormalizePathPipe
NormalizePathPipe,
DataCollectionComponent
]
})
export class UiModule {
Expand Down
Loading

0 comments on commit 72dbaea

Please sign in to comment.