简体   繁体   中英

Add Google analytics event tracking to javascript function

Is it possible to add google analytics event tracking to the folowing javascript function?

 $("#wScratchPad3").wScratchPad({

      scratchDown: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';},
      scratchMove: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';},
      scratchUp: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';}

    });

I use the following analytics code:

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-xx', 'xx');
ga('send', 'pageview');

</script>

Yeah, it's possible. You should use the latest version of Google Analytics (the analytics.js snippet). According to Google:

The analytics.js snippet is part of Universal Analytics, which is currently in public beta. New users should use analytics.js. Existing ga.js users should create a new web property for analytics.js and dual tag their site. It is perfectly safe to include both ga.js and analytics.js snippets on the same page.

once the analytics snippet is called on your page, you can just call the following function in your code wherever you want to track a particular event. You can give it custom category, action, and label names.

ga('send', 'event', 'category', 'action', 'label', value);  //label and value are optional

you can read all about it here: https://developers.google.com/analytics/devguides/collection/analyticsjs/events

in your example, you could do:

$("#wScratchPad3").wScratchPad({

    scratchDown: function(e, percent) {
        if(percent > 80) {
            ga('send', 'event', 'ScratchPad', 'scratchDown');
            window.location.href = 'http://www.url.com';
        }
    },
    scratchMove: function(e, percent) {
        if(percent > 80) {
            ga('send', 'event', 'ScratchPad', 'scratchMove');
            window.location.href = 'http://www.url.com';
        }
    },
    scratchUp: function(e, percent) {
        if(percent > 80) {
            ga('send', 'event', 'ScratchPad', 'scratchUp');
            window.location.href = 'http://www.url.com';
        }
    }

});

Depending on which Google Analytics script you're using, you may be able to get away with this. I formatted your code a bit as well to make it easier to read.

$("#wScratchPad3").wScratchPad({

    scratchDown: function(e, percent) {
        if(percent > 80) {
            _gaq.push(['_trackPageview', 'http://www.url.com']);
            window.location.href = 'http://www.url.com';
        }
    },
    scratchMove: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';},
    scratchUp: function(e, percent) {if(percent > 80)window.location.href = 'http://www.url.com';}

});

Can you post which analytics code you're using? Make sure to remove the UA code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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