UNPKG

14.9 kBMarkdownView Raw
1# Autotrack [![Build Status](https://travis-ci.org/googleanalytics/autotrack.svg?branch=master)](https://travis-ci.org/googleanalytics/autotrack)
2
3- [Overview](#overview)
4- [Plugins](#plugins)
5- [Installation and usage](#installation-and-usage)
6 - [Loading autotrack via npm](#loading-autotrack-via-npm)
7 - [Passing configuration options](#passing-configuration-options)
8- [Advanced configuration](#advanced-configuration)
9 - [Custom builds](#custom-builds)
10 - [Using autotrack with multiple trackers](#using-autotrack-with-multiple-trackers)
11- [Browser Support](#browser-support)
12- [Translations](#translations)
13
14## Overview
15
16The default [JavaScript tracking snippet](https://developers.google.com/analytics/devguides/collection/analyticsjs/) for Google Analytics runs when a web page is first loaded and sends a pageview hit to Google Analytics. If you want to know about more than just pageviews (e.g. where the user clicked, how far they scroll, did they see certain elements, etc.), you have to write code to capture that information yourself.
17
18Since most website owners care about a lot of the same types of user interactions, web developers end up writing the same code over and over again for every new site they build.
19
20Autotrack was created to solve this problem. It provides default tracking for the interactions most people care about, and it provides several convenience features (e.g. declarative event tracking) to make it easier than ever to understand how people are interacting with your site.
21
22## Plugins
23
24The `autotrack.js` file in this repository is small (7K gzipped) and comes with all plugins included. You can use it as is, or you can create a [custom build](#custom-builds) that only includes the plugins you want to make it even smaller.
25
26The following table briefly explains what each plugin does; you can click on the plugin name to see the full documentation and usage instructions:
27
28<table>
29 <tr>
30 <th align="left">Plugin</th>
31 <th align="left">Description</th>
32 </tr>
33 <tr>
34 <td><a href="/docs/plugins/clean-url-tracker.md"><code>cleanUrlTracker</code></a></td>
35 <td>Ensures consistency in the URL paths that get reported to Google Analytics; avoiding the problem where separate rows in your pages reports actually point to the same page.</td>
36 </tr>
37 <tr>
38 <td><a href="/docs/plugins/event-tracker.md"><code>eventTracker</code></a></td>
39 <td>Enables declarative event tracking, via HTML attributes in the markup.</td>
40 </tr>
41 <tr>
42 <td><a href="/docs/plugins/impression-tracker.md"><code>impressionTracker</code></a></td>
43 <td>Allows you to track when elements are visible within the viewport.</td>
44 </tr>
45 <tr>
46 <td><a href="/docs/plugins/max-scroll-tracker.md"><code>maxScrollTracker</code></a></td>
47 <td>Automatically tracks how far down the page a user scrolls.</td>
48 </tr>
49 <tr>
50 <td><a href="/docs/plugins/media-query-tracker.md"><code>mediaQueryTracker</code></a></td>
51 <td>Enables tracking media query matching and media query changes.</td>
52 </tr>
53 <tr>
54 <td><a href="/docs/plugins/outbound-form-tracker.md"><code>outboundFormTracker</code></a></td>
55 <td>Automatically tracks form submits to external domains.</td>
56 </tr>
57 <tr>
58 <td><a href="/docs/plugins/outbound-link-tracker.md"><code>outboundLinkTracker</code></a></td>
59 <td>Automatically tracks link clicks to external domains.</td>
60 </tr>
61 <tr>
62 <td><a href="/docs/plugins/page-visibility-tracker.md"><code>pageVisibilityTracker</code></a></td>
63 <td>Automatically tracks how long pages are in the visible state (as opposed to in a background tab)</td>
64 </tr>
65 <tr>
66 <td><a href="/docs/plugins/social-widget-tracker.md"><code>socialWidgetTracker</code></a></td>
67 <td>Automatically tracks user interactions with the official Facebook and Twitter widgets.</td>
68 </tr>
69 <tr>
70 <td><a href="/docs/plugins/url-change-tracker.md"><code>urlChangeTracker</code></a></td>
71 <td>Automatically tracks URL changes for single page applications.</td>
72 </tr>
73</table>
74
75**Disclaimer:** autotrack is maintained by members of the Google Analytics developer platform team and is primarily intended for a developer audience. It is not an official Google Analytics product and does not qualify for Google Analytics 360 support. Developers who choose to use this library are responsible for ensuring that their implementation meets the requirements of the [Google Analytics Terms of Service](https://www.google.com/analytics/terms/us.html) and the legal obligations of their respective country.
76
77## Installation and usage
78
79To add autotrack to your site, you have to do two things:
80
811. Load the `autotrack.js` script file included in this repo (or a [custom build](#custom-builds)) on your page.
822. Update your [tracking snippet](https://developers.google.com/analytics/devguides/collection/analyticsjs/tracking-snippet-reference) to [require](https://developers.google.com/analytics/devguides/collection/analyticsjs/using-plugins) the various autotrack plugins you want to use on the [tracker](https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers).
83
84If your site is currently using the [default JavaScript tracking snippet](https://developers.google.com/analytics/devguides/collection/analyticsjs/tracking-snippet-reference), you can modify it to something like this:
85
86```html
87<script>
88window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
89ga('create', 'UA-XXXXX-Y', 'auto');
90
91// Replace the following lines with the plugins you want to use.
92ga('require', 'eventTracker');
93ga('require', 'outboundLinkTracker');
94ga('require', 'urlChangeTracker');
95// ...
96
97ga('send', 'pageview');
98</script>
99<script async src="https://www.google-analytics.com/analytics.js"></script>
100<script async src="path/to/autotrack.js"></script>
101```
102
103Of course, you'll have to make the following modifications to the above code to customize autotrack to your needs:
104
105- Replace `UA-XXXXX-Y` with your [tracking ID](https://support.google.com/analytics/answer/1032385)
106- Replace the sample list of plugin `require` statements with the plugins you want to use.
107- Replace `path/to/autotrack.js` with the actual location of the `autotrack.js` file hosted on your server.
108
109**Note:** the [analytics.js plugin system](https://developers.google.com/analytics/devguides/collection/analyticsjs/using-plugins) is designed to support asynchronously loaded scripts, so it doesn't matter if `autotrack.js` is loaded before or after `analytics.js`. It also doesn't matter if the `autotrack.js` library is loaded individually or bundled with the rest of your JavaScript code.
110
111### Loading autotrack via npm
112
113If you use npm and a module loader that understands [ES2015 imports](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) (e.g. [Webpack](https://webpack.js.org/), [Rollup](http://rollupjs.org/), or [SystemJS](https://github.com/systemjs/systemjs)), you can include autotrack in your build by importing it as you would any other npm module:
114
115```sh
116npm install autotrack
117```
118
119```js
120// In your JavaScript code
121import 'autotrack';
122```
123**Note:** autotrack's source is published as ES2015, and you will need to make sure you're not excluding it from compilation. See [#137](https://github.com/googleanalytics/autotrack/issues/137) for more details.
124
125The above `import` statement will include all autotrack plugins in your generated source file. If you only want to include a specific set of plugins, you can import them individually:
126
127```js
128// In your JavaScript code
129import 'autotrack/lib/plugins/event-tracker';
130import 'autotrack/lib/plugins/outbound-link-tracker';
131import 'autotrack/lib/plugins/url-change-tracker';
132```
133
134The above examples show how to include the autotrack plugin source in your site's main JavaScript bundle, which accomplishes the first step of the [two-step installation process](#installation-and-usage). However, you still have to update your tracking snippet and require the plugins you want to use on the tracker.
135
136```js
137// Import just the plugins you want to use.
138import 'autotrack/lib/plugins/event-tracker';
139import 'autotrack/lib/plugins/outbound-link-tracker';
140import 'autotrack/lib/plugins/url-change-tracker';
141
142ga('create', 'UA-XXXXX-Y', 'auto');
143
144// Only require the plugins you've imported above.
145ga('require', 'eventTracker');
146ga('require', 'outboundLinkTracker');
147ga('require', 'urlChangeTracker');
148
149ga('send', 'pageview');
150```
151
152#### Code splitting
153
154Note that it's generally not a good idea to include any analytics as part of your site's main JavaScript bundle since analytics are not usually critical application functionality.
155
156If you're using a bundler that supports code splitting (via something like `System.import()`), it's best to load autotrack plugins lazily and delay their initialization until after your site's critical functionality has loaded:
157
158```js
159window.addEventListener('load', () => {
160 const autotrackPlugins = [
161 'autotrack/lib/plugins/event-tracker',
162 'autotrack/lib/plugins/outbound-link-tracker',
163 'autotrack/lib/plugins/url-change-tracker',
164 // List additional plugins as needed.
165 ];
166
167 Promise.all(autotrackPlugins.map((x) => System.import(x))).then(() => {
168 ga('create', 'UA-XXXXX-Y', 'auto');
169
170 ga('require', 'eventTracker', {...});
171 ga('require', 'outboundLinkTracker', {...});
172 ga('require', 'urlChangeTracker', {...});
173 // Require additional plugins imported above.
174
175 ga('send', 'pageview');
176 });
177})
178```
179
180If you're not sure how do use code splitting with your build setup, see the [custom builds](#custom-builds) section to learn how to manually generate a custom version of autotrack with just the plugins you need.
181
182### Passing configuration options
183
184All autotrack plugins accept a configuration object as the third parameter to the `require` command.
185
186Some of the plugins (e.g. `outboundLinkTracker`, `socialWidgetTracker`, `urlChangeTracker`) have a default behavior that works for most people without specifying any configuration options. Other plugins (e.g. `cleanUrlTracker`, `impressionTracker`, `mediaQueryTracker`) require certain configuration options to be set in order to work.
187
188See the individual plugin documentation to reference what options each plugin accepts (and what the default value is, if any).
189
190## Advanced configuration
191
192### Custom builds
193
194Autotrack comes with its own build system, so you can create autotrack bundles containing just the plugins you need. Once you've [installed autotrack via npm](#loading-autotrack-via-npm), you can create custom builds by running the `autotrack` command.
195
196For example, the following command generates an `autotrack.js` bundle and source map for just the `eventTracker`, `outboundLinkTracker`, and `urlChangeTracker` plugins:
197
198```sh
199autotrack -o path/to/autotrack.custom.js -p eventTracker,outboundLinkTracker,urlChangeTracker
200```
201
202Once this file is generated, you can include it in your HTML templates where you load `analytics.js`. Note the use of the `async` attribute on both script tags. This prevents `analytics.js` and `autotrack.custom.js` from interfering with the loading of the rest of your site.
203
204```html
205<script async src="https://www.google-analytics.com/analytics.js"></script>
206<script async src="path/to/autotrack.custom.js"></script>
207```
208
209### Using autotrack with multiple trackers
210
211All autotrack plugins support [multiple trackers](https://developers.google.com/analytics/devguides/collection/analyticsjs/creating-trackers#working_with_multiple_trackers) and work by specifying the tracker name in the `require` command. The following example creates two trackers and requires various autotrack plugins on each.
212
213```js
214// Creates two trackers, one named `tracker1` and one named `tracker2`.
215ga('create', 'UA-XXXXX-Y', 'auto', 'tracker1');
216ga('create', 'UA-XXXXX-Z', 'auto', 'tracker2');
217
218// Requires plugins on tracker1.
219ga('tracker1.require', 'eventTracker');
220ga('tracker1.require', 'socialWidgetTracker');
221
222// Requires plugins on tracker2.
223ga('tracker2.require', 'eventTracker');
224ga('tracker2.require', 'outboundLinkTracker');
225ga('tracker2.require', 'pageVisibilityTracker');
226
227// Sends the initial pageview for each tracker.
228ga('tracker1.send', 'pageview');
229ga('tracker2.send', 'pageview');
230```
231
232## Browser Support
233
234Autotrack will safely run in any browser without errors, as feature detection is always used with any potentially unsupported code. However, autotrack will only track features supported in the browser running it. For example, a user running Internet Explorer 8 will not be able to track media query usage, as media queries themselves aren't supported in Internet Explorer 8.
235
236All autotrack plugins are tested in the following browsers:
237
238<table>
239 <tr>
240 <td align="center">
241 <img src="https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/chrome/chrome_48x48.png" alt="Chrome"><br>
242
243 </td>
244 <td align="center">
245 <img src="https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/firefox/firefox_48x48.png" alt="Firefox"><br>
246
247 </td>
248 <td align="center">
249 <img src="https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/safari/safari_48x48.png" alt="Safari"><br>
250 6+
251 </td>
252 <td align="center">
253 <img src="https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/edge/edge_48x48.png" alt="Edge"><br>
254
255 </td>
256 <td align="center">
257 <img src="https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/archive/internet-explorer_9-11/internet-explorer_9-11_48x48.png" alt="Internet Explorer"><br>
258 9+
259 </td>
260 <td align="center">
261 <img src="https://raw.githubusercontent.com/alrra/browser-logos/39.2.2/src/opera/opera_48x48.png" alt="Opera"><br>
262
263 </td>
264 </tr>
265</table>
266
267## Translations
268
269The following translations have been graciously provided by the community. Please note that these translations are unofficial and may be inaccurate or out of date:
270
271* [Chinese](https://github.com/stevezhuang/autotrack/blob/master/README.zh.md)
272* [French](https://github.com/DirtyF/autotrack/tree/french-translation)
273* [Japanese](https://github.com/nebosuker/autotrack)
274* [Korean](https://github.com/youngilcho/autotrack/tree/korean-translation)
275* [Polish](https://github.com/krisu7/autotrack)
276
277If you discover issues with a particular translation, please file them with the appropriate repository. To submit your own translation, follow these steps:
278
2791. Fork this repository.
2802. Update the settings of your fork to [allow issues](http://programmers.stackexchange.com/questions/179468/forking-a-repo-on-github-but-allowing-new-issues-on-the-fork).
2813. Remove all non-documentation files.
2824. Update the documentation files with your translated versions.
2835. Submit a pull request to this repository that adds a link to your fork to the above list.