Manually generate GA linker parameter with gtag (GA4)

Viewed 124

Background

To pass a client_id from one domain to another, Google supports adding a "linker" parameter to outgoing Links that are part of the cross-domain tracking setup. This linker parameter contains the client_id, session_id (I believe, information about Google Ads, e.g. gclid) and a basic fingerprint + timestamp. On the receiving domain, if the browser fingerprint matches and the timestamp is not too far in the past, the passed client_id and session_id are stored in a first party cookie on the 2nd domain and consequently used.

analytics.js / GA-UA

With analytics.js (GA-UA) you could easily do the following, to decorate URLs manually:

function decorateUrl(urlString) {
  var ga = window[window['GoogleAnalyticsObject']];
  var tracker;
  if (ga && typeof ga.getAll === 'function') {
    tracker = ga.getAll()[0]; // Uses the first tracker created on the page
    urlString = (new window.gaplugins.Linker(tracker)).decorate(urlString);
  }
  return urlString;
}

Yet, when only gtag is loaded, window.ga and window.gaplugins are not defined. As far as I see, there is currently no documented way to manually generate links with the linker parameter with gtag.

In Google's documentation, they suggest setting up the linker manually. (https://support.google.com/analytics/answer/10071811?hl=en#zippy=%2Cmanual-setup) But this has several disadvantages, e.g. I have to create a custom "fingerprint" logic (so that decorated URLs are not shared) and e.g. Google Ads information is not included. Either way, I would like to use the internal gtag logic to decorate URLs.

"Hacky" Workaround Solution

gtag automatically decorates a tags (as soon as they're clicked) that lead to a cross-domain-tracking domain specified in the GA4 data stream settings (e.g. "test.com"), but I specifically need to decorate URLs manually (i.e. without immediately redirecting to them). I thought about doing the following:

  1. Create a dummy, hidden a tag with the URL to decorate
  2. Prevent redirection with onclick='event.preventDefault();'
  3. Simulate click on hidden element so that gtag automatically adds the linker url parameter to the href attribute
  4. Extract new href attribute
  5. Remove hidden element
function decorateUrlGtag(urlString) {
  var tempAnchorEl = document.createElement("a");
  tempAnchorEl.setAttribute("type", "hidden");
  tempAnchorEl.setAttribute("href", urlString);
  tempAnchorEl.setAttribute("onclick", "event.preventDefault(); return false");
  document.body.appendChild(tempAnchorEl);
  tempAnchorEl.click();
  var urlWithLinker = tempAnchorEl.href;
  tempAnchorEl.remove();
  return urlWithLinker;
}

This also does not work, because gtag does not seem to register the tempAnchorEl.click(); call. If I click the link manually, the URL is decorated - as expected.

Suggested Solutions

The solutions outlined here (Google Analytics gtag.js Manually adding the linker cross-domain parameter to URLs) also do not work for me:

  1. Answer: Even after gtag is initiated, I do not see a global ga element
  2. Answer: Same problem (no ga defined)

Do you (1) know if there is a way to generate the linker parameter manually with gtag that I have overlooked, (2) know how to make my "hacky" solution work or (3) have another possible solution?

0 Answers
Related