Enhanced link attribution

Enhanced link attribution improves click reports by automatically differentiating between multiple link clicks that have the same URL on a given page.

Usage

To use enhanced link attribution:

  1. Enable enhanced link attribution in the Admin UI of your Google Analytics account.
  2. Update your config command on each page to add the 'link_attribution': true parameter.

For example:

// Enable enhanced link attribution
gtag('config', 'GA_MEASUREMENT_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_MEASUREMENT_ID', {
  'link_attribution': {
    'cookie_name': '_gaela',
    'cookie_expires': 60,
    'levels': 2
  }
});