Skip to content

Commit

Permalink
NSIS installer id for analytics (n-air-app#310)
Browse files Browse the repository at this point in the history
* NSIS installer id for analytics

* remove pjson edit
  • Loading branch information
avacreeth authored Mar 2, 2018
1 parent b9924af commit 1029b53
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
46 changes: 41 additions & 5 deletions app/services/usage-statistics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Service } from './service';
import { Inject } from '../util/injector';
import { UserService } from './user';
import { HostsService } from './hosts';
import fs from 'fs';
import path from 'path';
import electron from 'electron';

export type TUsageEvent =
'stream_start' |
Expand All @@ -11,6 +14,7 @@ export type TUsageEvent =

interface IUsageApiData {
token?: string;
installer_id?: string;
slobs_user_id: string;
event: TUsageEvent;
data: object;
Expand All @@ -32,18 +36,46 @@ export function track(event: TUsageEvent) {


export class UsageStatisticsService extends Service {
@Inject() userService: UserService;
@Inject() hostsService: HostsService;

@Inject()
userService: UserService;
installerId: string;

@Inject()
hostsService: HostsService;
init() {
this.loadInstallerId();
}

loadInstallerId() {
let installerId = localStorage.getItem('installerId');

if (!installerId) {
const exePath = electron.remote.app.getPath('exe');
const installerNamePath = path.join(path.dirname(exePath), 'installername');

if (fs.existsSync(installerNamePath)) {
try {
const installerName = fs.readFileSync(installerNamePath).toString();

if (installerName) {
const matches = installerName.match(/\-([A-Za-z0-9]+)\.exe/);
if (matches) {
installerId = matches[1];
localStorage.setItem('installerId', installerId);
}
}
} catch (e) {
console.error('Error loading installer id', e);
}
}
}

this.installerId = installerId;
}

get isProduction() {
return process.env.NODE_ENV === 'production';
}


/**
* Record a usage event on our server.
* @param event the event type to record
Expand All @@ -65,6 +97,10 @@ export class UsageStatisticsService extends Service {
bodyData.token = this.userService.widgetToken;
}

if (this.installerId) {
bodyData.installer_id = this.installerId;
}

const request = new Request(`https://${this.hostsService.streamlabs}/api/v5/slobs/log`, {
method: 'POST',
headers,
Expand Down
5 changes: 5 additions & 0 deletions installer.nsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
!macro customInstall
FileOpen $0 "$INSTDIR\installername" w
FileWrite $0 $EXEFILE
FileClose $0
!macroend
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"license": "AGREEMENT",
"oneClick": false,
"allowToChangeInstallationDirectory": true,
"perMachine": true
"perMachine": true,
"include": "installer.nsh"
}
},
"ava": {
Expand Down

0 comments on commit 1029b53

Please sign in to comment.