OLD | NEW |
(Empty) | |
| 1 <h1 class="page_title">Tutorial: Google Analytics</h1> |
| 2 <p>This tutorial demonstrates using Google Analytics to track the usage of your |
| 3 extension.</p> |
| 4 <h2 id="toc-requirements">Requirements</h2> |
| 5 <p> |
| 6 This tutorial expects that you have some familiarity writing extensions for |
| 7 Google Chrome. If you need information on how to write an extension, please |
| 8 read the <a href="gettingstarted.html">Getting Started tutorial</a>. |
| 9 </p> |
| 10 <p> |
| 11 You will also need a <a href="http://www.google.com/analytics">Google |
| 12 Analytics account</a> set up to track your extension. Note that when setting |
| 13 up the account, you can use any value in the Website's URL field, as your |
| 14 extension will not have an URL of its own. |
| 15 </p> |
| 16 <p style="text-align: center"> |
| 17 <img src="{{static}}/images/tut_analytics/screenshot01.png" |
| 18 style="width:400px;height:82px;" |
| 19 alt="The analytics setup with info for a chrome extension filled out." /> |
| 20 </p> |
| 21 <h2 id="toc-installing">Installing the tracking code</h2> |
| 22 <p> |
| 23 The standard Google Analytics tracking code snippet fetches a file named |
| 24 <code>ga.js</code> from an SSL protected URL if the current page |
| 25 was loaded using the <code>https://</code> protocol. <strong>Chrome |
| 26 extensions and applications may <em>only</em> use the SSL-protected version of |
| 27 <code>ga.js</code></strong>. Loading <code>ga.js</code> over insecure HTTP is |
| 28 disallowed by Chrome's default <a href="contentSecurityPolicy.html">Content |
| 29 Security Policy</a>. This, plus the fact that Chrome extensions are hosted |
| 30 under the <code>chrome-extension://</code> schema, requires a slight |
| 31 modification to the usual tracking snippet to pull <code>ga.js</code> directly |
| 32 from <code>https://ssl.google-analytics.com/ga.js</code> instead of the |
| 33 default location. |
| 34 </p> |
| 35 <p> |
| 36 Below is a modified snippet for the |
| 37 <a href="http://code.google.com/apis/analytics/docs/tracking/asyncTracking.htm
l">asynchronous |
| 38 tracking API</a> (the modified line is bolded): |
| 39 </p> |
| 40 <pre> |
| 41 (function() { |
| 42 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.asy
nc = true; |
| 43 <strong>ga.src = 'https://ssl.google-analytics.com/ga.js';</strong> |
| 44 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(
ga, s); |
| 45 })(); |
| 46 </pre> |
| 47 <p> |
| 48 You'll also need to ensure that your extension has access to load the resource |
| 49 by relaxing the default content security policy. The policy definition in your |
| 50 <a href="manifest.html"><code>manifest.json</code></a> might look like: |
| 51 </p> |
| 52 <pre>{ |
| 53 ..., |
| 54 "content_security_policy": "script-src 'self' https://ssl.google-analytics.com
; object-src 'self'", |
| 55 ... |
| 56 }</pre> |
| 57 <p> |
| 58 Here is a popup page (<code>popup.html</code>) which loads the asynchronous |
| 59 tracking code via an external JavaScript file (<code>popup.js</code>) and |
| 60 tracks a single page view: |
| 61 </p> |
| 62 <pre>popup.js: |
| 63 ========= |
| 64 var _gaq = _gaq || []; |
| 65 _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']); |
| 66 _gaq.push(['_trackPageview']); |
| 67 (function() { |
| 68 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.asy
nc = true; |
| 69 ga.src = 'https://ssl.google-analytics.com/ga.js'; |
| 70 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(
ga, s); |
| 71 })(); |
| 72 popup.html: |
| 73 =========== |
| 74 <!DOCTYPE html> |
| 75 <html> |
| 76 <head> |
| 77 ... |
| 78 <script src="popup.js"></script> |
| 79 </head> |
| 80 <body> |
| 81 ... |
| 82 </body> |
| 83 </html> |
| 84 </pre> |
| 85 <p> |
| 86 Keep in mind that the string <code>UA-XXXXXXXX-X</code> should be replaced |
| 87 with your own Google Analytics account number. |
| 88 </p> |
| 89 <h2 id="toc-tracking-pageviews">Tracking page views</h2> |
| 90 <p> |
| 91 The <code>_gaq.push(['_trackPageview']);</code> code will track a single |
| 92 page view. This code may be used on any page in your extension. When |
| 93 placed on a background page, it will register a view once per browser |
| 94 session. When placed on a popup, it will register a view once every time |
| 95 the popup is opened. |
| 96 </p> |
| 97 <p> |
| 98 By looking at the page view data for each page in your extension, you can |
| 99 get an idea of how many times your users interact with your extension per |
| 100 browser session: |
| 101 </p> |
| 102 <p style="text-align: center"> |
| 103 <img src="{{static}}/images/tut_analytics/screenshot02.png" |
| 104 style="width:300px;height:119px;" |
| 105 alt="Analytics view of the top content for a site." /> |
| 106 </p> |
| 107 <h2 id="toc-debugging">Monitoring analytics requests</h2> |
| 108 <p> |
| 109 To ensure that tracking data from your extension is being sent to Google |
| 110 Analytics, you can inspect the pages of your extension in the |
| 111 Developer Tools window (see the |
| 112 <a href="tut_debugging.html">debugging tutorial</a> for more information). |
| 113 As the following figure shows, you should see requests for a file named |
| 114 <strong>__utm.gif</strong> if everything is set up correctly. |
| 115 </p> |
| 116 <p style="text-align: center"> |
| 117 <img src="{{static}}/images/tut_analytics/screenshot04.png" |
| 118 style="width:683px;height:418px;" |
| 119 alt="Developer Tools window showing the __utm.gif request" /> |
| 120 </p> |
| 121 <h2 id="toc-tracking-events">Tracking events</h2> |
| 122 <p> |
| 123 By configuring event tracking, you can determine which parts of your |
| 124 extension your users interact with the most. For example, if you have |
| 125 three buttons users may click: |
| 126 </p> |
| 127 <pre> |
| 128 <button id='button1'>Button 1</button> |
| 129 <button id='button2'>Button 2</button> |
| 130 <button id='button3'>Button 3</button> |
| 131 </pre> |
| 132 <p> |
| 133 Write a function that sends click events to Google Analytics: |
| 134 </p> |
| 135 <pre> |
| 136 function trackButton(e) { |
| 137 _gaq.push(['_trackEvent', e.target.id, 'clicked']); |
| 138 }; |
| 139 </pre> |
| 140 <p> |
| 141 And use it as an event handler for each button's click: |
| 142 </p> |
| 143 <pre> |
| 144 var buttons = document.querySelectorAll('button'); |
| 145 for (var i = 0; i < buttons.length; i++) { |
| 146 buttons[i].addEventListener('click', trackButtonClick); |
| 147 } |
| 148 </pre> |
| 149 <p> |
| 150 The Google Analytics event tracking overview page will give you metrics |
| 151 regarding how many times each individual button is clicked: |
| 152 </p> |
| 153 <p style="text-align: center"> |
| 154 <img src="{{static}}/images/tut_analytics/screenshot03.png" |
| 155 style="width:300px;height:482px;" |
| 156 alt="Analytics view of the event tracking data for a site." /> |
| 157 </p> |
| 158 <p> |
| 159 By using this approach, you can see which parts of your extension are |
| 160 under-or-overutilized. This information can help guide decisions about UI |
| 161 redesigns or additional functionality to implement. |
| 162 </p> |
| 163 <p> |
| 164 For more information about using the event tracking API, see the |
| 165 Google Analytics |
| 166 <a href="http://code.google.com/apis/analytics/docs/tracking/eventTrackerOverv
iew.html">developer |
| 167 documentation</a>. |
| 168 </p> |
| 169 <h2 id="toc-samplecode">Sample code</h2> |
| 170 <p> |
| 171 A sample extension that uses these techniques is |
| 172 available in the Chromium source tree: |
| 173 </p> |
| 174 <blockquote> |
| 175 <a href="http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensi
ons/docs/examples/tutorials/analytics/">.../examples/tutorials/analytics/</a> |
| 176 </blockquote> |
| 177 </p> |
OLD | NEW |