- Installation and usage
- Advanced configuration
- Browser Support
Since 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.
Autotrack 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.
autotrack.js file in this repository is small (8K gzipped) and comes with all plugins included. You can use it as is, or you can create a custom build that only includes the plugins you want to make it even smaller.
The following table briefly explains what each plugin does; you can click on the plugin name to see the full documentation and usage instructions:
||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.|
||Enables declarative event tracking, via HTML attributes in the markup.|
||Allows you to track when elements are visible within the viewport.|
||Automatically tracks how far down the page a user scrolls.|
||Enables tracking media query matching and media query changes.|
||Automatically tracks form submits to external domains.|
||Automatically tracks link clicks to external domains.|
||Automatically tracks how long pages are in the visible state (as opposed to in a background tab)|
||Automatically tracks user interactions with the official Facebook and Twitter widgets.|
||Automatically tracks URL changes for single page applications.|
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 and the legal obligations of their respective country.
Installation and usage
To add autotrack to your site, you have to do two things:
- Load the
autotrack.jsscript file included in this repo (or a custom build) on your page.
- Update your tracking snippet to require the various autotrack plugins you want to use on the tracker.
Of course, you'll have to make the following modifications to the above code to customize autotrack to your needs:
UA-XXXXX-Ywith your tracking ID
- Replace the sample list of plugin
requirestatements with the plugins you want to use.
path/to/autotrack.jswith the actual location of the
autotrack.jsfile hosted on your server.
Note: the analytics.js plugin system 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
Loading autotrack via npm
npm install autotrack
Note: autotrack's source is published as ES2015, and you will need to make sure you're not excluding it from compilation. See #137 for more details.
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:
// Import just the plugins you want to use.;;;;// Only require the plugins you've imported above.;;;;
If 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:
If you're not sure how do use code splitting with your build setup, see the custom builds section to learn how to manually generate a custom version of autotrack with just the plugins you need.
Passing configuration options
All autotrack plugins accept a configuration object as the third parameter to the
Some of the plugins (e.g.
urlChangeTracker) have a default behavior that works for most people without specifying any configuration options. Other plugins (e.g.
mediaQueryTracker) require certain configuration options to be set in order to work.
See the individual plugin documentation to reference what options each plugin accepts (and what the default value is, if any).
Autotrack 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, you can create custom builds by running the
For example, the following command generates an
autotrack.js bundle and source map for just the
autotrack -o path/to/autotrack.custom.js -p eventTracker,outboundLinkTracker,urlChangeTracker
Once 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
autotrack.custom.js from interfering with the loading of the rest of your site.
Using autotrack with multiple trackers
All autotrack plugins support 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.
// Creates two trackers, one named `tracker1` and one named `tracker2`.;;// Requires plugins on tracker1.;;// Requires plugins on tracker2.;;;// Sends the initial pageview for each tracker.;;
Autotrack 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.
All autotrack plugins are tested in the following browsers:
The following translations have been graciously provided by the community. Please note that these translations are unofficial and may be inaccurate or out of date:
If you discover issues with a particular translation, please file them with the appropriate repository. To submit your own translation, follow these steps:
- Fork this repository.
- Update the settings of your fork to allow issues.
- Remove all non-documentation files.
- Update the documentation files with your translated versions.
- Submit a pull request to this repository that adds a link to your fork to the above list.