Skip to content

Commit 4713060

Browse files
committed
feat: add ga dev id and change to live on grunt pack
1 parent a14fd05 commit 4713060

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

Gruntfile.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
const childProcess = require("child_process");
2+
const EOL = require("os").EOL;
23
const now = new Date().toISOString();
34

5+
const CONFIG_JSON_PATH = "config/config.json";
6+
7+
const ENVIRONMENTS = {
8+
live: "live",
9+
dev: "dev"
10+
};
11+
12+
const GA_TRACKING_IDS = {
13+
[ENVIRONMENTS.dev]: "UA-111455-51",
14+
[ENVIRONMENTS.live]: "UA-111455-44"
15+
};
16+
17+
const CONFIG_DATA = {
18+
filePath: "config/config.json",
19+
gaKey: "GA_TRACKING_ID"
20+
}
21+
422
function shallowCopy(obj) {
523
var result = {};
624
Object.keys(obj).forEach(function (key) {
@@ -191,17 +209,43 @@ module.exports = function (grunt) {
191209
grunt.file.write("package.json", JSON.stringify(packageJson, null, " "));
192210
});
193211

212+
const setConfig = (key, value) => {
213+
const configJson = grunt.file.readJSON(CONFIG_DATA.filePath);
214+
configJson[key] = value;
215+
const stringConfigContent = JSON.stringify(configJson, null, " ") + EOL;
216+
grunt.file.write(CONFIG_DATA.filePath, stringConfigContent);
217+
}
218+
219+
grunt.registerTask("set_live_ga_id", function() {
220+
setConfig(CONFIG_DATA.gaKey, GA_TRACKING_IDS[ENVIRONMENTS.live]);
221+
});
222+
223+
grunt.registerTask("set_dev_ga_id", function() {
224+
setConfig(CONFIG_DATA.gaKey, GA_TRACKING_IDS[ENVIRONMENTS.dev]);
225+
});
226+
227+
grunt.registerTask("verify_live_ga_id", function() {
228+
var configJson = grunt.file.readJSON(CONFIG_DATA.filePath);
229+
230+
if(configJson[CONFIG_DATA.gaKey] !== GA_TRACKING_IDS[ENVIRONMENTS.live]) {
231+
throw new Error("Google Analytics id is not configured correctly.");
232+
}
233+
});
234+
194235
grunt.registerTask("test", ["ts:devall", "shell:npm_test"]);
195236
grunt.registerTask("pack", [
196237
"clean",
197238
"ts:release_build",
198239
"shell:npm_test",
199240

200241
"set_package_version",
242+
"set_live_ga_id",
243+
"verify_live_ga_id",
201244
"shell:build_package",
202245

203246
"copy:package_to_drop_folder",
204-
"copy:package_to_qa_drop_folder"
247+
"copy:package_to_qa_drop_folder",
248+
"set_dev_ga_id"
205249
]);
206250
grunt.registerTask("lint", ["tslint:build"]);
207251
grunt.registerTask("all", ["clean", "test", "lint"]);

config/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
"DISABLE_HOOKS": false,
88
"UPLOAD_PLAYGROUND_FILES_ENDPOINT": "https://play.nativescript.org/api/files",
99
"SHORTEN_URL_ENDPOINT": "https://play.nativescript.org/api/shortenurl?longUrl=%s",
10-
"PREVIEW_APP_ENVIRONMENT": "live"
10+
"PREVIEW_APP_ENVIRONMENT": "live",
11+
"GA_TRACKING_ID": "UA-111455-51"
1112
}

lib/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class Configuration extends ConfigBase implements IConfiguration { // Use
1111
UPLOAD_PLAYGROUND_FILES_ENDPOINT: string = null;
1212
SHORTEN_URL_ENDPOINT: string = null;
1313
PREVIEW_APP_ENVIRONMENT: string = null;
14+
GA_TRACKING_ID: string = null;
1415

1516
/*don't require logger and everything that has logger as dependency in config.js due to cyclic dependency*/
1617
constructor(protected $fs: IFileSystem) {

lib/declarations.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ interface IConfiguration extends Config.IConfig {
412412
UPLOAD_PLAYGROUND_FILES_ENDPOINT: string;
413413
SHORTEN_URL_ENDPOINT: string;
414414
PREVIEW_APP_ENVIRONMENT: string;
415+
GA_TRACKING_ID: string;
415416
}
416417

417418
interface IApplicationPackage {

lib/services/analytics/google-analytics-provider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ import * as ua from "universal-analytics";
33
import { AnalyticsClients } from "../../common/constants";
44

55
export class GoogleAnalyticsProvider implements IGoogleAnalyticsProvider {
6-
private static GA_TRACKING_ID = "UA-111455-44";
76
private currentPage: string;
87

98
constructor(private clientId: string,
109
private $staticConfig: IStaticConfig,
1110
private $analyticsSettingsService: IAnalyticsSettingsService,
1211
private $logger: ILogger,
13-
private $proxyService: IProxyService) {
12+
private $proxyService: IProxyService,
13+
private $config: IConfiguration) {
1414
}
1515

1616
public async trackHit(trackInfo: IGoogleAnalyticsData): Promise<void> {
1717
const sessionId = uuid.v4();
1818

1919
try {
20-
await this.track(GoogleAnalyticsProvider.GA_TRACKING_ID, trackInfo, sessionId);
20+
await this.track(this.$config.GA_TRACKING_ID, trackInfo, sessionId);
2121
} catch (e) {
2222
this.$logger.trace("Analytics exception: ", e);
2323
}

0 commit comments

Comments
 (0)