Google Analytics Event Tracking via a jQuery plugi

2019-07-09 17:05发布

问题:

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.