问题
I have added a snippet of javascript to my pdf download links in order to track downloads. When I watch RealTime Events from the GA dashboard, I don't see any activity when I click on a download link. My links have the following code:
<a class='non-html' href='pdf/XXXX_2017_meeting_program.pdf' onclick=”var that=this;_gaq.push([‘_trackEvent’,’Download’,’PDF’,this.href]);setTimeout(function(){location.href=that.href;},200);return false;”>2017: 18th Annual Symposium, Chicago, IL, USA</a>
Is the link properly formed and am I looking in the correct place to observe event activity in real time?
Follow-Up: OK, I've kept it simple, just the command "event" and 1 parameter, "Download". I still don't see any activity. I have the most current gtag:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src='https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXX-XX'></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXX--XX');
</script>`
and the link codes is as follows:
<a class='non-html' href='pdf/XXXX_2017_meeting_program.pdf' onclick = 'gtag ('event', 'Download');' >2017: 18th Annual Symposium, Chicago, IL, USA</a>
Do I need to do anything else?
回答1:
Depending on which library you use - analytics.js or gtag.js, correct your code:
onclick ="ga('send', 'event', <category>, <action>, <label>, <value>);"
or
onclick = "gtag('event', <action>, {'event_category': <category>,'event_label': <label>,'value': <value>});"
To see event in real time use report Real Time - Events.
来源:https://stackoverflow.com/questions/52048136/google-analytics-tracking-code-doesnt-record-downloads-in-realtime