简体   繁体   English

Google Analytics(分析)/跟踪代码管理器-事件跟踪-我很困惑

[英]Google Analytics / Tag Manager - Event Tracking - I'm confused

I'm developing a webpage and have been asked to instrument the back end to record user clicks on links. 我正在开发一个网页,并被要求检测后端以记录用户对链接的点击。 I found google analytics and thought that would provide all the tracking they could ever want. 我找到了Google Analytics(分析),并认为这将提供他们可能想要的所有跟踪。 So I set up a Google analytics account. 因此,我建立了一个Google Analytics(分析)帐户。 In the head of the webpage I added: 在网页的开头,我添加了:

<script>
    window.dataLayer = window.dataLayer || [];
    function gtag() { dataLayer.push(arguments); }
    gtag('js', new Date());
    gtag('config', 'UA-MYCODEXX-1');
</script>

I've then added a button to my page like so: 然后,我在页面上添加了一个按钮,如下所示:

<a href="didyouknow.html" class="btn btn-primary" onclick="trackOutboundLink(this, 'Internal Links', 'Did You Know'); return false;">More</a>

I've then been trying to figure out how to track link clicks. 然后,我一直试图弄清楚如何跟踪链接点击。 I've come across 3 differing approaches and I don't know what to use in my case: 我遇到了3种不同的方法,但我不知道该如何使用:

function trackOutboundLink(link, category, action) {
    try {
        _gaq.push(['_trackEvent', category, action]);// OPTION 1
        ga('send', 'event', category, action);       // OPTION 2
        gtag('event', category, action);             // OPTION 3
    } catch (err) {
    }
    setTimeout(function () {
        document.location.href = link.href;
    }, 100);
}

It seems from what I've read that OPTION 1 is outdated. 从我阅读的内容来看,选项1已过时。 OPTION 3 seems most in keeping with the gtag code in the script I had to add. 选项3似乎最符合我必须添加的脚本中的gtag代码。 But then I'm not sure if that requires also subscribing to Google Tag Manager? 但是我不确定这是否还需要订阅Google跟踪代码管理器? It's difficult for me to test because a system administrator has to deploy my webpages and scripts to the server and is not very responsive at present. 我很难测试,因为系统管理员必须将我的网页和脚本部署到服务器上,并且目前响应速度并不快。 Can I also test that this works when running the webpage locally on my PC? 在我的PC上本地运行网页时,我还可以测试这种方法是否有效吗? Thanks 谢谢

Gtag - is Google's newest implementation for the Analytics API for web. Gtag-Google针对Web的Analytics API的最新实现。 It doesn't require Google double click to function (but is based around the doubleclick code so allows for easier integration later should you choose to use it). 它不需要Google双击即可起作用(但它基于doubleclick代码,因此以后您选择使用它时,可以更轻松地进行集成)。

To track a link with this method: 要使用此方法跟踪链接:

function trackOutboundLink(link, category, action) {
    try {

       gtag('event', 'play', {
         'send_to': 'UA-MYCODEXX-1',
         'event_category': 'Videos',
         'event_label': 'Fall Campaign'
       });

    } catch (err) {
    }
    setTimeout(function () {
        document.location.href = link.href;
    }, 100);
}

See the migration guide for help on the differences that this newest version brings (versus other code you may have found on the web). 请参阅迁移指南以获取有关此最新版本带来的差异的帮助(与您可能在网络上找到的其他代码相比)。 https://developers.google.com/analytics/devguides/collection/gtagjs/migration https://developers.google.com/analytics/devguides/collection/gtagjs/migration

As for debugging - there's a Chrome plugin for Analytics over here: 至于调试-这里有一个适用于Google Analytics(分析)的Chrome插件:

https://chrome.google.com/webstore/detail/page-analytics-by-google/fnbdnhhicmebfgdgglcdacdapkcihcoh?hl=en https://chrome.google.com/webstore/detail/page-analytics-by-google/fnbdnhhicmebfgdgglcdacdapkcihcoh?hl=en

I've successfully used this to debug local events previously as it will give some output in the console. 我以前已经成功地使用它来调试本地事件,因为它将在控制台中提供一些输出。

Your google analytics script is correct and including this inside the head is correct. 您的Google Analytics(分析)脚本是正确的,并且将其包含在头部内是正确的。

 <script>
    window.dataLayer = window.dataLayer || [];
    function gtag() { dataLayer.push(arguments); }
    gtag('js', new Date());
    gtag('config', 'UA-MYCODEXX-1');
</script>

Event tracking would be gtag when using the latest google analytics script. 使用最新的Google Analytics(分析)脚本时,事件跟踪将为gtag。

gtag('event', 'Title goes here', {'event_category': 'Category goes here','event_label': 'Label goes here'});

Best way to test this would be login to your GA account, on this specific property under the reports menu on the left you will find Real-Time. 最好的测试方法是登录您的Google Analytics(分析)帐户,在左侧“报告”菜单下的此特定属性上,您会发现“实时”。 Under Real-time you have the events tab. 在实时下,您具有事件选项卡。

Clicking on your onClick event from your website should then trigger an event to show inside the events tab, if you see this happening you know your events are firing. 如果从网站上单击onClick事件,则应触发一个事件以显示在“事件”选项卡中,如果您看到这种情况,则知道事件正在触发。 This can be tested from local PC. 可以从本地PC进行测试。

This can be done with Tag Manager as well but its a different setup process. 标记管理器也可以完成此操作,但设置过程有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM