Skip to content

Commit 008e04d

Browse files
committed
Add Google Analytics tracking
1 parent bc0a3e6 commit 008e04d

File tree

8 files changed

+139
-6
lines changed

8 files changed

+139
-6
lines changed

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
"license": "SEE LICENSE IN LICENSE.txt",
2727
"dependencies": {
2828
"source-map": "^0.5.3",
29-
"vscode-debugprotocol": "^1.7.0",
30-
"vscode-debugadapter": "^1.7.0"
29+
"universal-analytics": "^0.3.11",
30+
"vscode-debugadapter": "^1.7.0",
31+
"vscode-debugprotocol": "^1.7.0"
3132
},
3233
"devDependencies": {
3334
"mocha": "^2.4.5",
@@ -87,7 +88,7 @@
8788
"typescript"
8889
]
8990
},
90-
"program": "./out/webkit/webKitDebug.js",
91+
"program": "./out/debug-adapter/webKitDebug.js",
9192
"runtime": "node",
9293
"initialConfigurations": [
9394
{

src/.vscode/launch.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "launch as server",
66
"type": "node",
77
"request": "launch",
8-
"program": "${workspaceRoot}/../out/webkit/webKitDebug.js",
8+
"program": "${workspaceRoot}/../out/debug-adapter/webKitDebug.js",
99
"runtimeArgs": ["--nolazy"],
1010
"args": [ "--server=4712" ],
1111
"stopOnEntry": true,
@@ -20,7 +20,7 @@
2020
// Path to VSCode executable
2121
"runtimeExecutable": "${execPath}",
2222
"args": [
23-
"--extensionDevelopmentPath=${workspaceRoot}"
23+
"--extensionDevelopmentPath=${workspaceRoot}/.."
2424
],
2525
"stopOnEntry": false,
2626
"sourceMaps": true,

src/debug-adapter/webKitDebugAdapter.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {formatConsoleMessage} from './consoleHelper';
1414
import * as ns from '../services/NsCliService';
1515
import {ILaunchRequestArgs, IAttachRequestArgs, IDebugAdapter, ISetBreakpointsArgs, ISetBreakpointsResponseBody, IBreakpoint,
1616
IStackTraceResponseBody, IScopesResponseBody, IVariablesResponseBody, ISourceResponseBody, IThreadsResponseBody, IEvaluateResponseBody} from './WebKitAdapterInterfaces';
17+
import {AnalyticsService} from '../services/analytics/AnalyticsService';
1718

1819

1920
interface IScopeVarHandle {
@@ -114,6 +115,7 @@ export class WebKitDebugAdapter implements IDebugAdapter {
114115
}
115116

116117
private _attach(args: IAttachRequestArgs | ILaunchRequestArgs) {
118+
AnalyticsService.getInstance().launchDebugger(args.request, args.platform, args.emulator);
117119
this.initDiagnosticLogging(args.request, args);
118120
this.appRoot = utils.getAppRoot(args);
119121
this.platform = args.platform;

src/main.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
22
import * as child from 'child_process';
33
import * as ns from './services/NsCliService';
44
import {ExtensionVersionInfo} from './services/ExtensionVersionInfo';
5+
import {AnalyticsService} from './services/analytics/AnalyticsService';
56

67
function performVersionsCheck(context: vscode.ExtensionContext) {
78
// Check the state of the existing NativeScript CLI
@@ -37,7 +38,7 @@ function performVersionsCheck(context: vscode.ExtensionContext) {
3738
}
3839
}
3940

40-
// this method is called when your extension is activated
41+
// this method is called when the extension is activated
4142
export function activate(context: vscode.ExtensionContext) {
4243
performVersionsCheck(context);
4344

@@ -64,6 +65,7 @@ export function activate(context: vscode.ExtensionContext) {
6465

6566
// Execute run command
6667
let emulator: boolean = (target === 'emulator');
68+
AnalyticsService.getInstance().runRunCommand(project.platform(), emulator);
6769
return project.run(emulator)
6870
.then(tnsProcess => {
6971
tnsProcess.on('error', err => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export enum OperatingSystem {
2+
Windows,
3+
Linux,
4+
OSX,
5+
Other
6+
}
7+
8+
export interface AnalyticsBaseInfo {
9+
operatingSystem: OperatingSystem,
10+
cliVersion: string,
11+
extensionVersion: string,
12+
userId: string,
13+
hostname: string
14+
}
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import * as os from 'os';
2+
import { Version } from '../../common/Version';
3+
import { GUAService } from './GUAService';
4+
import { AnalyticsBaseInfo, OperatingSystem } from './AnalyticsBaseInfo';
5+
import { ExtensionVersionInfo } from '../ExtensionVersionInfo';
6+
import * as ns from '../NsCliService';
7+
8+
export class AnalyticsService {
9+
private static _instance: AnalyticsService;
10+
11+
private _baseInfo: AnalyticsBaseInfo;
12+
private _gua: GUAService;
13+
14+
public static getInstance(): AnalyticsService {
15+
if (!this._instance) {
16+
this._instance = new AnalyticsService();
17+
}
18+
return this._instance;
19+
}
20+
21+
public static generateMachineId(): string {
22+
let machineId = '';
23+
try {
24+
let netInterfaces = os.networkInterfaces();
25+
Object.keys(netInterfaces).forEach(interfName => {
26+
netInterfaces[interfName].forEach(interf => {
27+
if (!interf.internal) {
28+
machineId += `${interf.mac}-`;
29+
}
30+
});
31+
});
32+
} catch(e) {}
33+
return machineId;
34+
}
35+
36+
constructor() {
37+
let operatingSystem = OperatingSystem.Other;
38+
switch(process.platform) {
39+
case 'win32': { operatingSystem = OperatingSystem.Windows; break; }
40+
case 'darwin': { operatingSystem = OperatingSystem.OSX; break; }
41+
case 'linux':
42+
case 'freebsd': { operatingSystem = OperatingSystem.Linux; break; }
43+
};
44+
45+
this._baseInfo = {
46+
cliVersion: Version.stringify(ns.CliVersionInfo.getInstalledCliVersion()),
47+
extensionVersion: Version.stringify(ExtensionVersionInfo.getExtensionVersion()),
48+
operatingSystem: operatingSystem,
49+
userId: AnalyticsService.generateMachineId(),
50+
hostname: 'ns-vs-extension.org'
51+
};
52+
53+
this._gua = new GUAService('UA-111455-29', this._baseInfo);
54+
}
55+
56+
public launchDebugger(request: string, platform: string, emulator: boolean): Promise<any> {
57+
try {
58+
return this._gua.launchDebugger(request, platform, emulator);
59+
} catch(e) {}
60+
}
61+
62+
public runRunCommand(platform: string, emulator: boolean): Promise<any> {
63+
try {
64+
return this._gua.runRunCommand(platform, emulator);
65+
} catch(e) {}
66+
}
67+
}

src/services/analytics/GUAService.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as ua from 'universal-analytics';
2+
import { AnalyticsBaseInfo, OperatingSystem } from './AnalyticsBaseInfo';
3+
4+
/**
5+
* Google Universal Analytics Service
6+
*/
7+
export class GUAService {
8+
private _visitor: any;
9+
private _getBasePayload: () => any;
10+
11+
constructor(trackingId: string, baseInfo: AnalyticsBaseInfo) {
12+
this._visitor = ua(trackingId);
13+
this._getBasePayload = () => {
14+
return {
15+
uid: baseInfo.userId,
16+
dh: baseInfo.hostname,
17+
cd5: baseInfo.cliVersion,
18+
cd6: OperatingSystem[baseInfo.operatingSystem],
19+
cd7: baseInfo.extensionVersion
20+
};
21+
};
22+
}
23+
24+
public launchDebugger(request: string, platform: string, emulator: boolean): Promise<any> {
25+
let payload = this._getBasePayload();
26+
payload.ec = 'vscode-extension-debug'; // event category
27+
payload.ea = `debug-${request}-on-${platform}-${emulator ? 'emulator' : 'device'}`; // event action
28+
return this.sendEvent(payload);
29+
}
30+
31+
public runRunCommand(platform: string, emulator: boolean): Promise<any> {
32+
let payload = this._getBasePayload();
33+
payload.ec = 'vscode-extension-command'; // event category
34+
payload.ea = `command-run-on-${platform}-${emulator ? 'emulator' : 'device'}`; // event action
35+
return this.sendEvent(payload);
36+
}
37+
38+
private sendEvent(params): Promise<any> {
39+
return new Promise<any>((res, rej) => {
40+
this._visitor.event(params, err => {
41+
return err ? rej(err) : res();
42+
});
43+
});
44+
}
45+
}

src/typings.json

+2
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010
"globalDevDependencies": {
1111
"mocha": "registry:dt/mocha#2.2.5+20160317120654"
1212
},
13+
"dependencies": {
14+
"universal-analytics": "registry:npm/universal-analytics#0.3.10+20160419033917"
1315
}
1416
}

0 commit comments

Comments
 (0)