Google Analytics Event Tracking via a jQuery plugin

梦想与她 提交于 2019-12-12 21:31:58

问题


The proof of concept started with this:

<script type="text/javascript">
    $(document).ready(function() {
    $('.evnttrk1').click(function() {
    _gaq.push(['_trackEvent', 'coming_soon', 'footer_icons', $(this).attr('id')]);
});
});

With this, any link with a class of eventrk1 would trigger a Google Analytics Event. This worked just fine.

From there I decide to roll it into a proper plugin, adding some functionality along the way. Everything about the plugin works except I can't get the _gaq.push to send events to Google. I'm not getting an error. I'm not getting anything at all.

I have the plugin running on this coming soon page: http://AcaciaNJ.com

I'd like to tell you this gave me gray hairs but that's not possible since it made me pull all my hair out. Please help. This is really starting to bother me.


回答1:


Maybe try window._gaq.push?

You can detect if the _gaq object is available by testing for window._gaq:

if (window._gaq) {
    alert('_gaq found');
} else {
    alert('_gaq not found');
}

You could also pass an arbitrary function to window._gaq.push() to help with debugging:

window._gaq.push(function() {alert('Pushed function to _gaq')});

I also noticed that your script passes a fifth argument to _trackEvent, val. This has to be an integer, and tracking will not work if it is a string.



来源:https://stackoverflow.com/questions/8845188/google-analytics-event-tracking-via-a-jquery-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!