From a727e107fe2d5f86f110dd54917656aeea318941 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 27 Sep 2016 22:44:42 -0400 Subject: [PATCH] setup google analytics with pageview tracking --- gulpfile.js | 6 ++++++ package.json | 4 +++- src/background.js | 1 + src/manifest.json | 5 ++++- src/popup/app/app.js | 2 ++ src/popup/index.html | 4 ++++ src/scripts/analytics.js | 26 ++++++++++++++++++++++++++ src/scripts/analyticsCode.js | 5 +++++ 8 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/scripts/analytics.js create mode 100644 src/scripts/analyticsCode.js diff --git a/gulpfile.js b/gulpfile.js index 2171d379..7a0d1deb 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -106,6 +106,12 @@ gulp.task('lib', ['clean:lib'], function () { src: [paths.npmDir + 'sweetalert/dist/sweetalert.css', paths.npmDir + 'sweetalert/dist/sweetalert.min.js', paths.npmDir + 'angular-sweetalert/SweetAlert.js'], dest: paths.libDir + 'sweetalert' + }, + { + src: [paths.npmDir + 'angulartics-google-analytics/lib/angulartics*.js', + paths.npmDir + 'angulartics/src/angulartics.js' + ], + dest: paths.libDir + 'angulartics' } ]; diff --git a/package.json b/package.json index 7c221049..0f86f4b4 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,8 @@ "q": "1.4.1", "angularjs-slider": "5.5.0", "sweetalert": "1.1.3", - "angular-sweetalert": "1.1.1" + "angular-sweetalert": "1.1.1", + "angulartics": "1.2.1", + "angulartics-google-analytics": "0.2.1" } } diff --git a/src/background.js b/src/background.js index 539299d2..93bff0ec 100644 --- a/src/background.js +++ b/src/background.js @@ -1,3 +1,4 @@ +var isBackground = true; var utilsService = new UtilsService(); var cryptoService = new CryptoService(); var tokenService = new TokenService(); diff --git a/src/manifest.json b/src/manifest.json index eadb9b9e..b72d802e 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -12,6 +12,7 @@ "96": "images/icon96.png", "128": "images/icon128.png" }, + "content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'", "content_scripts": [ { "js": [ "content/autoFill.js" ], @@ -32,6 +33,7 @@ "lib/sjcl/bitArray.js", "lib/q/q.js", "scripts/tld.js", + "scripts/analyticsCode.js", "models/api/requestModels.js", "models/api/responseModels.js", "models/dataModels.js", @@ -47,7 +49,8 @@ "services/autofillService.js", "services/appIdService.js", "services/passwordGenerationService.js", - "background.js" + "background.js", + "scripts/analytics.js" ], "persistent": true }, diff --git a/src/popup/app/app.js b/src/popup/app/app.js index 397e3641..6c203b27 100644 --- a/src/popup/app/app.js +++ b/src/popup/app/app.js @@ -3,6 +3,8 @@ 'ui.router', 'ngAnimate', 'toastr', + 'angulartics', + 'angulartics.google.analytics', 'bit.directives', 'bit.services', diff --git a/src/popup/index.html b/src/popup/index.html index 07f96dc8..16bb3c52 100644 --- a/src/popup/index.html +++ b/src/popup/index.html @@ -14,6 +14,8 @@ + + @@ -23,6 +25,8 @@ + + diff --git a/src/scripts/analytics.js b/src/scripts/analytics.js new file mode 100644 index 00000000..db04d450 --- /dev/null +++ b/src/scripts/analytics.js @@ -0,0 +1,26 @@ +var gaUtils = chrome.extension.getBackgroundPage().utilsService, + gaTrackingId = null; + +if (gaUtils.isChrome()) { + gaTrackingId = 'UA-81915606-6'; +} +else if (gaUtils.isFirefox()) { + gaTrackingId = 'UA-81915606-7'; +} +else if (gaUtils.isEdge()) { + gaTrackingId = 'UA-81915606-9'; +} +else if (gaUtils.isOpera()) { + gaTrackingId = 'UA-81915606-8'; +} + +if (gaTrackingId) { + ga('create', gaTrackingId, 'auto'); +} + +// Removes failing protocol check. ref: http://stackoverflow.com/a/22152353/1958200 +ga('set', 'checkProtocolTask', function () { }); + +if (typeof isBackground !== 'undefined') { + ga('send', 'pageview', '/background.html'); +} diff --git a/src/scripts/analyticsCode.js b/src/scripts/analyticsCode.js new file mode 100644 index 00000000..d17b25d2 --- /dev/null +++ b/src/scripts/analyticsCode.js @@ -0,0 +1,5 @@ +(function (i, s, o, g, r, a, m) { +i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { +(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), +m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) +})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');