使用javascript手动设置gtag utm参数

时间:2019-06-28 13:17:56

标签: google-analytics gtag.js utm-tracking

为了更好地跟踪跨域iframe,我想手动为Google Analytics(分析)的gtag设置utm参数。

我无法更改添加utm_source = {source}等的网址。

我的iframe的加载方式如下:

<iframe src="https://example.com/?pagetest=a&source=source_page"></iframe>

然后在服务器端,我将提取这些参数,以便如果另一个父站点放置了Referer_source而不是源,我可以更改此参数,以便在javascript中所有变量都相同。

我找到了this thread,但是它不适合我。这是标题中的脚本代码:

     <script async src="https://www.googletagmanager.com/gtag/js?id={id}"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', '{id}',{
        'page_title':'title',
        'page_referrer':'{ source }',
        'page_location':'/current_page',
        'page_path':'/current_page',
        });

        // Sends an event that passes 'age' as a parameter.
        gtag('event', 'page_view', {'pagetest': 'a'});
    </script>

我仍然将iframe的父域视为引荐,并且在“事件”标签上我只能看到

事件类别:常规; 事件动作:pagetest; 事件:100%

我想在页面上进行总计的分析,然后在示例中对pagetest a和pagetest b进行细分。

如何在javascript中设置所有这些点?

1 个答案:

答案 0 :(得分:0)

您可以尝试类似的方法,告诉我它与您的用例相距多远:

    <script async src="https://www.googletagmanager.com/gtag/js?id={id}"></script>
<script>
    window.dataLayer = window.dataLayer || [];

    function gtag() {
        dataLayer.push(arguments);
    }
    // set everything once for every susbsequent hits
    // add source information to LOCATION throught utm_referrer
    // clear the referrer value to prevent interfering with the value you're adding through utm_referrer
    gtag('set', {
        'page_location': '/current_page' + '?utm_referrer=' + encodeURIComponent(source),
        'page_referrer': null,
        'page_path': '/current_page'
    });
    gtag('js', new Date());
    gtag('config', '{id}', {
        'page_title': 'initial page title'
    });

    // Send another page view 
    gtag('event', 'page_view', {
        'page_path': '/virtual_page_path_value',
        'page_title': 'title for virtual page view'
    });
</script>