Autotrack Documentation
repository·master·Indexed 26 days ago
https://github.com/googleanalytics/autotrackA library providing automated and enhanced Google Analytics tracking for common web user interactions such as clicks, scrolls, and visibility. It features a plugin-based architecture including trackers for impressions, outbound links, page visibility, and URL changes in SPAs. Version 2.4.1 supports installation via script tag or npm, custom bundle generation via CLI, and configuration for multiple Google Analytics trackers.
What's inside autotrack
- Autotrack provides default tracking for common user interactions (like clicks, scrolling, and element visibility) that the standard Google Analytics JavaScript snippet does not capture automatically. It includes several plugins to simplify event tracking and user behavior analysis.
Autotrack Documentation Overview
masterAutotrack is a library designed to automate the tracking of various user interactions and browser states for Google Analytics. This documentation provides a central hub for understanding the library's core functionality, common configuration options, and detailed references for its plugin ecosystem.Enable the `socialWidgetTracker` plugin
masterThe
socialWidgetTrackerplugin automatically tracks user interactions with official Twitter tweet/follow buttons and the Facebook like button. To enable it, use thega('require', ...)command.In most cases, no customization is required.
ga('require', 'socialWidgetTracker');Install and use Autotrack via script tag
masterTo use Autotrack with a standard tracking snippet, follow these two steps:
- Load the
autotrack.jsfile on your page. - Use the
ga('require', 'pluginName')command in your tracking snippet to enable the plugins you want.
Note:
autotrack.jscan be loaded before or afteranalytics.jsdue to the asynchronous nature of the plugin system.<script> window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; ga('create', 'UA-XXXXX-Y', 'auto'); // Enable specific plugins ga('require', 'eventTracker'); ga('require', 'outboundLinkTracker'); ga('require', 'urlChangeTracker'); ga('send', 'pageview'); </script> <script async src="https://www.google-analytics.com/analytics.js"></script> <script async src="path/to/autotrack.js"></script>- Load the
Install Autotrack via npm
masterIf you use a module loader that supports ES2015 imports (like Webpack or Rollup), you can install Autotrack via npm.
Important: Autotrack source is published as ES2015; ensure your build process does not exclude it from compilation.
To include all plugins, import
autotrack. To reduce bundle size, import only the specific plugins you need fromautotrack/lib/plugins/.npm install autotrack// To include all plugins import 'autotrack'; // To include specific plugins only import 'autotrack/lib/plugins/event-tracker'; import 'autotrack/lib/plugins/outbound-link-tracker'; import 'autotrack/lib/plugins/url-change-tracker'; // After importing, you must still require them on the tracker ga('create', 'UA-XXXXX-Y', 'auto'); ga('require', 'eventTracker'); ga('require', 'outboundLinkTracker'); ga('require', 'urlChangeTracker'); ga('send', 'pageview');Calculate Page Visible Time metrics
masterTo effectively use the data from
pageVisibilityTracker, create a custom metric in Google Analytics calledPage Visible Time(using the index provided invisibleMetricIndex).You can then create calculated metrics to analyze engagement:
Avg. Page Visible Time (per Session): Use this for session-level dimensions (e.g., Referrer, Device Category).
- Formula:
{{Page Visible Time}} / {{Sessions}}
- Formula:
Avg. Page Visible Time (per Page): Use this for page-specific dimensions (e.g., Page, Title).
- Formula:
{{Page Visible Time}} / {{Unique Pageviews}}
- Formula:
Require plugins individually in 1.x.x and 2.x.x
masterIn versions 1.0.0 and later, you can no longer use the shorthand
ga('require', 'autotrack')to load all plugins at once. You must explicitly require each plugin you intend to use. This prevents accidental activation of unwanted plugin behaviors.Note: In version 2.0.0, calling
ga('require', 'autotrack')may prevent subsequent commands from running. Always require specific plugins instead.<script> window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; ga('create', 'UA-XXXXX-Y', 'auto'); // Plugins must be required individually. ga('require', 'cleanUrlTracker', {...}); ga('require', 'eventTracker', {...}); ga('require', 'impressionTracker', {...}); ga('require', 'mediaQueryTracker', {...}); ga('require', 'outboundFormTracker', {...}); ga('require', 'outboundLinkTracker', {...}); ga('require', 'pageVisibilityTracker', {...}); ga('require', 'socialWidgetTracker', {...}); ga('require', 'urlChangeTracker', {...}); // ... ga('send', 'pageview'); </script> <script async src="https://www.google-analytics.com/analytics.js"></script> <script async src="path/to/autotrack.js"></script>Preserve site search parameters with cleanUrlTracker
masterWhen using
stripQuery: true, site search parameters (likeq) will be removed from thepagefield. Since Google Analytics does not automatically infer site search from thelocationfield when apagefield is present, you must whitelist these parameters usingqueryParamsWhitelistto ensure search data is still captured.ga('require', 'cleanUrlTracker', { stripQuery: true, queryParamsWhitelist: ['q'], });Use the mediaQueryTracker plugin
masterThe
mediaQueryTrackerplugin tracks responsive design media queries and sends them to Google Analytics as custom dimensions. It also sends event hits when media query values change (e.g., when a user resizes their browser).To enable the plugin, use the
ga('require', ...)command with a configuration object containingdefinitions.ga('require', 'mediaQueryTracker', options);Upgrade from 1.x.x to 2.x.x
masterWhen upgrading to version 2.x.x, be aware of the following breaking changes:
- Module Syntax: The source code now uses ES2015 module syntax to enable tree shaking. This may cause compatibility issues with bundlers that do not natively support this syntax, such as older versions of Browserify.
cleanUrlTracker: Calls totracker.get('page')will now return the 'cleaned' version of the page.impressionTracker: Events sent by this plugin are nownonInteractionby default.pageVisibilityTracker:- The
changeevent action is no longer used. Instead, events with atrackaction are sent when the page transitions out of the visible state to record visibility time. - The
changeTemplateoption has been removed. - The
hiddenMetricIndexoption has been removed.
- The
Integrate the urlChangeTracker plugin
masterThe
urlChangeTrackerplugin detects URL changes via the History API and automatically sends pageview hits. This is designed for Single Page Applications (SPAs) to track navigation without manual configuration.Note: This plugin does not support tracking hash changes. Ensure your SPA framework is not already tracking URL changes to avoid duplicate data.
ga('require', 'urlChangeTracker', options);Integrate the pageVisibilityTracker plugin
masterThe
pageVisibilityTrackerplugin improves engagement measurement by using the Page Visibility API instead of relying solely on page loads. It tracks how long a page is visible and can automatically trigger new sessions or pageviews when a user returns to a tab.To enable the plugin, use the
ga('require', ...)command. If you configure the plugin to handle the initial pageview, you must remove the manualga('send', 'pageview')command from your tracking snippet to avoid duplicate hits.ga('require', 'pageVisibilityTracker', options);