Add Google Analytics to track when users switch to PolyGerrit

Change-Id: I4dd601d2ebbba9f9e34ba3405820ff43c0e409c4
diff --git a/background.js b/background.js
index 0be98a1..2e5080b 100644
--- a/background.js
+++ b/background.js
@@ -12,6 +12,28 @@
 // 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(['_trackPageview']);
+
+(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);
+})();
+
+var RequestType = {
+  NAV: 'nav',
+};
+
+chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
+  if (request.type == RequestType.NAV) {
+    _gaq.push(['_trackPageview', request.url]);
+  }
+});
+
 function loadImage(path, width, height) {
   return new Promise(function(resolve) {
     var canvas = document.createElement('canvas');
@@ -82,6 +104,7 @@
     };
     chrome.cookies.get(cookieID, function(cookie) {
       if (cookie) {
+        _gaq.push(['_trackEvent', 'Page Action', 'Switch to Gerrit']);
         chrome.cookies.remove(cookieID, function() {
           // The GWT UI does not handle PolyGerrit URL redirection.
           chrome.tabs.update(tab.id, {
@@ -91,11 +114,12 @@
           });
         });
       } else {
+        _gaq.push(['_trackEvent', 'Page Action', 'Switch to PolyGerrit']);
         chrome.cookies.set({
           url: tab.url,
           name: 'GERRIT_UI',
           value: 'polygerrit',
-          httpOnly: true ,
+          httpOnly: true,
           path: '/',
           secure: true,
         }, function() {
diff --git a/content.js b/content.js
new file mode 100644
index 0000000..0ad4756
--- /dev/null
+++ b/content.js
@@ -0,0 +1,24 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+(function() {
+  // TODO(andybons): Integrate with the Gerrit JS API. Since this is loaded
+  // before the Gerrit object is in scope, this type of integration may require
+  // an event to be fired to indicate that the Gerrit object is available.
+  var appEl = document.querySelector('gr-app');
+  var isPolyGerrit = !!appEl;
+  if (isPolyGerrit) {
+    // TODO(andybons): Track actions and integrate with the Gerrit JS API.
+    // PageJS isn't in scope and is a pain to latch on to for navigation events.
+  }
+})();
diff --git a/manifest.json b/manifest.json
index 26959d8..1fc4d20 100644
--- a/manifest.json
+++ b/manifest.json
@@ -3,7 +3,7 @@
 
   "name": "Gerrit UI Switcher",
   "description": "Easily switch between the current Gerrit UI and the new PolyGerrit UI",
-  "version": "0.3",
+  "version": "0.4",
   "background": {
     "scripts": ["background.js"]
   },
@@ -14,11 +14,20 @@
     },
     "default_title": "Toggle PolyGerrit UI"
   },
+  "content_scripts": [
+    {
+      "matches": ["*://*.googlesource.com/*"],
+      "js": [
+        "content.js"
+      ]
+    }
+  ],
   "permissions": [
     "cookies",
     "declarativeContent",
     "tabs",
     "webRequest",
     "*://*.googlesource.com/*"
-  ]
+  ],
+  "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'"
 }