开发者

how to use google analytics to count clicks on a button

开发者 https://www.devze.com 2022-12-23 18:34 出处:网络
I have google analytics on my site. One page has 开发者_开发技巧a button which when pressed executes some javascript.

I have google analytics on my site. One page has 开发者_开发技巧a button which when pressed executes some javascript. It would be nice to monitor the number of hits the button receives when people come to this page. Can anybody suggest the easiest way to achieve this with google analytics ? Are there any best practices to do this ?

thanks


You can trackPageview in the link's onclick handler: http://www.google.com/support/googleanalytics/bin/answer.py?answer=55521

<a href="javascript:void(0);"onClick="javascript:pageTracker._trackPageview('/folder/file');" >

This inflates your pageviews though, so it may be better to so use GA event tracking: http://code.google.com/apis/analytics/docs/tracking/eventTrackerOverview.html

<a href="#" onClick="pageTracker._trackEvent('Videos', 'Play', 'Baby\'s First Birthday');">Play</a>


Updated Answer

Here is the new method for Google Analytics event tracking:

<button onClick="ga('send', 'event', 'Category', 'Action', 'Label');" >Button text</button>
  • Category: Typically the object that was interacted with (e.g. 'Video')
  • Action: The type of interaction (e.g. 'play')
  • Label (optional param): Useful for categorizing events (e.g. 'Fall Campaign')

More info here: https://developers.google.com/analytics/devguides/collection/analyticsjs/events


The google analytics snippet sends google the page URL (among other things, probably) every time it's executed. I don't think you will be able to use it to count button clicks. If you execute the same snippet, it will keep sending the page's URL which is hardly valuable. If you manage to change the URL it sends, you might be able to get something...

Update:

You were right and I was wrong: google analytics does in fact support tracking events other than page loads.

0

精彩评论

暂无评论...
验证码 换一张
取 消