Enhanced link attribution

Google Analytics provides enhanced link attribution to improve the accuracy of report data by automatically differentiating between multiple links to the same URL on a single page by using link element IDs.

Usage

To use the enhanced link attribution feature:

  1. Enable enhanced link attribution in the Admin UI of your Google Analytics account.
  2. Update your tracking code on each page to enable enhanced link attribution with the 'link_attribution': true setting.

For example:

// Enable enhanced link attribution
gtag('config', 'GA_TRACKING_ID', {
  'link_attribution': true
});

Enhanced link attribution uses the element IDs of a link or a parent element and a cookie to differentiate between links to the same URL. You can customize how far up the DOM the plug-in will look for an element ID, as well as the behavior of this cookie, by providing configuration options when loading the plug-in.

Here are the available options and their defaults:

Option Data type Default Description
cookie_name string _gali The name of the cookie
cookie_expires number 30 The maximum duration (in seconds) the cookie should be saved for
levels number 3

The maximum number of levels in the DOM to look to find an existing ID. For example, the following links do not contain ID attributes, but the <ul> element (two levels up) does:

<ul id="sidebar">
  <li><a href="/">Home</a></li>
  <li><a href="/about">About</a></li>
  <li><a href="/contact">Contact Us</a></li>
</ul>

If the levels option were set to 1, the "sidebar" ID would not be found and the link would remain anonymous.

For example, the following illustrates how code might look with every possible option customized:

// Turn on enhanced link attribution with every option customized
gtag('config', 'GA_TRACKING_ID', {
  'link_attribution': {
    'cookie_name': '_gaela',
    'cookie_expires': 60,
    'levels': 2
  }
});