新的 gtag 谷歌分析手动添加 url _gl

时间:2021-04-21 14:48:26

标签: google-analytics gtag.js

我正在尝试将 _gl 的值添加到 URL 以应用跨域。

目前,我尝试了以下选项:

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;
  }

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);
}

但这些都没有将 _gl 代码设置为 URL。

我在标题中注入这个代码

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

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

在 GA4 属性中,建议通过 url 传递 client_id 和 session_id:https://support.google.com/analytics/answer/10071811?hl=en#zippy=%2Cmanual-setup