New gtag google analytics add url _gl manually

2.4k Views Asked by At

I'm trying to add to URL the value of _gl in order to apply cross-domain.

Currently, I tried these options:

const decorateUrl = (urlString: string) => {
    var ga = window.dataLayer;
    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;
  }

and

const decorateURL = (url: string) => {
    let destinationLink = false;
    var ga = window[window['GoogleAnalyticsObject']];
    console.log(ga,'ga test decorate')
    if (ga) {
       let tracker = ga.getAll()[0];
       let linker = new window.gaplugins.Linker(tracker);
       destinationLink = linker.decorate(url);
    }

    return (destinationLink ? url + '?' + destinationLink : url);
}

but none of these set _gl code to URL.

and I inject in the header this code

let gtagLinkScript = destinationWindow.document.createElement("script");
    gtagLinkScript.setAttribute('async', 'true');
    gtagLinkScript.setAttribute('src', `https://www.googletagmanager.com/gtag/js?id=${ trackingID }`);

and

let gtagAnotherScript = destinationWindow.document.createElement("script");
    gtagAnotherScript.innerText = `gtag('set', 'linker', {'domains': [\'${crossDomain}\']});gtag(\'config\', \'${ trackingID }\');`;

can someone help?

1

There are 1 best solutions below

0
On

In a GA4 property the recommendation is to pass client_id and session_id over the url: https://support.google.com/analytics/answer/10071811?hl=en#zippy=%2Cmanual-setup