Skip to content

Commit 8040341

Browse files
authored
Merge pull request #4155 from NativeScript/kddimitrov/add-test-ga-project
feat: add ga dev id and change to live on grunt pack
2 parents 4a084b9 + 17403f5 commit 8040341

File tree

6 files changed

+56
-20
lines changed

6 files changed

+56
-20
lines changed

Gruntfile.js

Lines changed: 47 additions & 16 deletions
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) {
@@ -101,17 +119,6 @@ module.exports = function (grunt) {
101119

102120
},
103121

104-
copy: {
105-
package_to_drop_folder: {
106-
src: "*.tgz",
107-
dest: "<%= copyPackageTo %>/<%= jobName %>/<%= dateString %> #<%= buildNumber %>/"
108-
},
109-
package_to_qa_drop_folder: {
110-
src: "*.tgz",
111-
dest: "<%= copyPackageTo %>/<%= jobName %>/nativescript.tgz"
112-
}
113-
},
114-
115122
clean: {
116123
src: ["test/**/*.js*",
117124
"lib/**/*.js*",
@@ -191,17 +198,41 @@ module.exports = function (grunt) {
191198
grunt.file.write("package.json", JSON.stringify(packageJson, null, " "));
192199
});
193200

201+
const setConfig = (key, value) => {
202+
const configJson = grunt.file.readJSON(CONFIG_DATA.filePath);
203+
configJson[key] = value;
204+
const stringConfigContent = JSON.stringify(configJson, null, " ") + EOL;
205+
grunt.file.write(CONFIG_DATA.filePath, stringConfigContent);
206+
}
207+
208+
grunt.registerTask("set_live_ga_id", function() {
209+
setConfig(CONFIG_DATA.gaKey, GA_TRACKING_IDS[ENVIRONMENTS.live]);
210+
});
211+
212+
grunt.registerTask("set_dev_ga_id", function() {
213+
setConfig(CONFIG_DATA.gaKey, GA_TRACKING_IDS[ENVIRONMENTS.dev]);
214+
});
215+
216+
grunt.registerTask("verify_live_ga_id", function() {
217+
var configJson = grunt.file.readJSON(CONFIG_DATA.filePath);
218+
219+
if(configJson[CONFIG_DATA.gaKey] !== GA_TRACKING_IDS[ENVIRONMENTS.live]) {
220+
throw new Error("Google Analytics id is not configured correctly.");
221+
}
222+
});
223+
194224
grunt.registerTask("test", ["ts:devall", "shell:npm_test"]);
195-
grunt.registerTask("pack", [
225+
grunt.registerTask("prepare", [
196226
"clean",
197227
"ts:release_build",
198228
"shell:npm_test",
199229

230+
"set_live_ga_id",
231+
"verify_live_ga_id"
232+
]);
233+
grunt.registerTask("pack", [
200234
"set_package_version",
201-
"shell:build_package",
202-
203-
"copy:package_to_drop_folder",
204-
"copy:package_to_qa_drop_folder"
235+
"shell:build_package"
205236
]);
206237
grunt.registerTask("lint", ["tslint:build"]);
207238
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
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"test": "node test-scripts/istanbul.js",
1515
"postinstall": "node postinstall.js",
1616
"preuninstall": "node preuninstall.js",
17+
"prepack": "grunt prepare",
18+
"postpack": "grunt set_dev_ga_id",
1719
"mocha": "node test-scripts/mocha.js",
1820
"tsc": "tsc",
1921
"tslint": "tslint -p tsconfig.json --type-check",

0 commit comments

Comments
 (0)