Upgrade google analytics; fix page reporting
- Drop deprecated ga.js in favor of analytics.js
- Queue timing reports on page load until page change is reported in
order to correctly connect timings to the page
- Report version and gerrit domain
Change-Id: Ia2b0bef936a06770fb7fd428705854396580ed29
diff --git a/background.js b/background.js
index 7f038a8..3b0652f 100644
--- a/background.js
+++ b/background.js
@@ -12,18 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-var _gaq = _gaq || [];
-_gaq.push(['_setAccount', 'UA-73551813-1']);
-_gaq.push(['_setSiteSpeedSampleRate', 100]);
+(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');
-(function() {
- var ga = document.createElement('script');
- ga.type = 'text/javascript';
- ga.async = true;
- ga.src = 'https://ssl.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0];
- s.parentNode.insertBefore(ga, s);
-})();
+ga('create', 'UA-73551813-1', {
+ 'siteSpeedSampleRate': 100,
+ 'cookieDomain': 'none',
+});
+ga('set', 'checkProtocolTask', null);
+ga('send', 'pageview');
var RequestType = {
NAV: 'nav-report',
@@ -32,17 +32,25 @@
chrome.runtime.onMessage.addListener(function(request) {
var detail = request.detail;
- if (detail.host) {
- _gaq.push(['_setDomainName', detail.host]);
- }
+ var version = detail.ver || chrome.app.getDetails().version;
+
+ ga('set', 'dimension1', version);
+ ga('set', 'dimension2', detail.host);
switch (request.type) {
case RequestType.TIMING:
var time = Math.round(detail.value);
- _gaq.push(['_trackTiming', detail.category, detail.name, time]);
+ var hourInMillis = 1000 * 60 * 60;
+ if (0 < time && time < hourInMillis) {
+ ga('send', 'timing', detail.category, detail.name, time);
+ } else {
+ console.log(
+ 'timing out of range: ' + [detail.category, detail.name, time]);
+ }
break;
case RequestType.NAV:
- _gaq.push(['_trackPageview', detail.value]);
+ ga('set', 'page', detail.value);
+ ga('send', 'pageview');
break;
};
});
@@ -197,7 +205,7 @@
};
chrome.cookies.get(cookieID, function(cookie) {
if (cookie) {
- _gaq.push(['_trackEvent', 'Page Action', 'Switch to Gerrit']);
+ ga('send', 'event', 'Page Action', 'Switch to Gerrit');
chrome.cookies.remove(cookieID, function() {
// The GWT UI does not handle PolyGerrit URL redirection.
chrome.tabs.update(tab.id, {
@@ -207,7 +215,7 @@
});
});
} else {
- _gaq.push(['_trackEvent', 'Page Action', 'Switch to PolyGerrit']);
+ ga('send', 'event', 'Page Action', 'Switch to PolyGerrit');
chrome.cookies.set({
url: tab.url,
name: 'GERRIT_UI',
diff --git a/content.js b/content.js
index 55a2804..1b24124 100644
--- a/content.js
+++ b/content.js
@@ -16,12 +16,30 @@
var isPolyGerrit = !!appEl;
if (isPolyGerrit) {
+ var page;
+ var pendingEvents = [];
var reportToBackground = function(e) {
e.detail.host = location.host;
chrome.runtime.sendMessage({type: e.type, detail: e.detail});
};
- document.addEventListener('timing-report', reportToBackground);
- document.addEventListener('nav-report', reportToBackground);
+ var reportNav = function(e) {
+ page = e.detail.value;
+ reportToBackground(e);
+ if (pendingEvents.length) {
+ pendingEvents.forEach(reportToBackground);
+ pendingEvents = [];
+ }
+ };
+ var reportTiming = function(e) {
+ if (page) {
+ reportToBackground(e);
+ } else {
+ pendingEvents.push(
+ {type: e.type, detail: Object.assign({}, e.detail)});
+ }
+ };
+ document.addEventListener('timing-report', reportTiming);
+ document.addEventListener('nav-report', reportNav);
} else {
chrome.runtime.sendMessage({
type: 'nav-report',
diff --git a/manifest.json b/manifest.json
index 39a68fb..62d9a4c 100644
--- a/manifest.json
+++ b/manifest.json
@@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Gerrit UI Switcher",
"description": "Easily switch between the current Gerrit UI and the new PolyGerrit UI",
- "version": "0.5.2",
+ "version": "0.5.3",
"background": {
"scripts": [
"background.js"
@@ -45,5 +45,5 @@
"*://*.git.corp.google.com/*",
"*://*.staging-git.corp.google.com/*"
],
- "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'"
+ "content_security_policy": "script-src 'self' https://www.google-analytics.com; object-src 'self'"
}