Autotrack Documentation

repository·master·Indexed 26 days ago

https://github.com/googleanalytics/autotrack

A 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.

Tokens
15K
Snippets
42
Records
93
Agent score
88%

What's inside autotrack

  1. Overview of Autotrack

    master
    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.
  2. Autotrack Documentation Overview

    master
    Autotrack 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.
  3. Install and use Autotrack via script tag

    master

    To use Autotrack with a standard tracking snippet, follow these two steps:

    1. Load the autotrack.js file on your page.
    2. Use the ga('require', 'pluginName') command in your tracking snippet to enable the plugins you want.

    Note: autotrack.js can be loaded before or after analytics.js due 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>
  4. Install Autotrack via npm

    master

    If 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 from autotrack/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');
  5. Calculate Page Visible Time metrics

    master

    To effectively use the data from pageVisibilityTracker, create a custom metric in Google Analytics called Page Visible Time (using the index provided in visibleMetricIndex).

    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}}
    • Avg. Page Visible Time (per Page): Use this for page-specific dimensions (e.g., Page, Title).

      • Formula: {{Page Visible Time}} / {{Unique Pageviews}}
  6. Require plugins individually in 1.x.x and 2.x.x

    master

    In 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>
  7. Preserve site search parameters with cleanUrlTracker

    master

    When using stripQuery: true, site search parameters (like q) will be removed from the page field. Since Google Analytics does not automatically infer site search from the location field when a page field is present, you must whitelist these parameters using queryParamsWhitelist to ensure search data is still captured.

    ga('require', 'cleanUrlTracker', {
      stripQuery: true,
      queryParamsWhitelist: ['q'],
    });
  8. Use the mediaQueryTracker plugin

    master

    The mediaQueryTracker plugin 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 containing definitions.

    ga('require', 'mediaQueryTracker', options);
  9. Upgrade from 1.x.x to 2.x.x

    master

    When 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 to tracker.get('page') will now return the 'cleaned' version of the page.
    • impressionTracker: Events sent by this plugin are now nonInteraction by default.
    • pageVisibilityTracker:
      • The change event action is no longer used. Instead, events with a track action are sent when the page transitions out of the visible state to record visibility time.
      • The changeTemplate option has been removed.
      • The hiddenMetricIndex option has been removed.
  10. Integrate the urlChangeTracker plugin

    master

    The urlChangeTracker plugin 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);
  11. Integrate the pageVisibilityTracker plugin

    master

    The pageVisibilityTracker plugin 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 manual ga('send', 'pageview') command from your tracking snippet to avoid duplicate hits.

    ga('require', 'pageVisibilityTracker', options);