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