how to use google analytics to count clicks on a button - google-analytics

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
Play

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.

Related

Is it possible to detect keypresses using Google Tag Manager (GTM)?

The idea is to check if someone started filling in the form but hasn't submitted it. Is it possible to detect physical keypresses with GTM?
Yes, quite easily, with the following:
HTML:
<input type="text" onkeyup="dataLayer.push({event:'textChanged', eventAction: this.value, eventLabel: location.href, eventValue: 0.0});">
Note that the eventAction, eventLabel and eventValue are optional and customizable.
GTM Tag: Google Analytics, "Event"
GTM trigger: Custom Event: "textChanged"
GTM is Javascript, and in Javascript it is possible to detect key presses, so it is possible to detect key presses in GTM by using a custom HTML tag to implement your own keyup/keydown event handler. Documentation for the event handler is here.
A more typical way to detect form interaction is to implement event handler for the change event on text fields. Warning, this might lead to excessive firing of events (but then, so might tracking keystrokes).

Google Analytics Event Tracking not Appearing in Real Time

This event and all onclick events like it are not tracking in Google Analytics Real Time. Any idea why not?
email#domain.com
This is not specific to mailto links. This is for any link where I'm trying to use onclick event tracking.
In case you're wondering I do not have an IP filter set.
We're using gatag.js (newer version of ga.js). I was still using the syntax from ga.js The syntax for tracking an event is slightly different in the newer gatag.js. That was my issue.

Google Analytics Event firing too often

I have a page setup with the following GA JavaScript on the page to track pageviews
ga('create', 'UA-xxxx-xxx', 'auto');
ga('send', 'pageview', location.pathname);
I also have the following JavaScript within document.ready to track an Event
$(document).on('click', 'button[type="submit"]', function () {
if (typeof (ga) !== 'undefined') {
ga('send', 'event', 'Your Details', 'Save Address Change');
}
});
Within all development and test environments, the code fires only when it should (the form contains only ONE button) but in LIVE, the Event is almost in line with the pageview number (and the backend data confirms that the button has not been pressed as it is logged in the SQL database as it fires an INSERT statement). When I attempt to debug the JS, the event does NOT fire
Is there any way the pageview event on my form could then be triggering the event? Caching?
Using the secondary dimensions, I cannot find any data that tells me what or who is triggering this event on almost every pageview. All it confirms is that this is not isolated to one browser
The code looks like it would operate the way you would hope, so there must be something else going on... This isn't particularly an answer as much as it is other things to check that may lead to an answer:
How many Unique Events vs. Total Events are occurring for "Your Details"?
Are there any other submit buttons elsewhere on the page (search, subscribe, etc)?
Any chance you're loading your tracking script (where the event listener is) more than once?
How about spam bots? Have you noticed many server-side crawlers? This wouldn't be caused by "ghost" spambots, so check for Full Referrer as a secondary dimension.
Does this appear on both your unfiltered and filtered views?
Are any other core metrics (users, sessions, pageviews, bounce rate) in your reports odd?
Create a segment for just users who have triggered this event and look through all of your other reports. Are they following a typical funnel/path that you would expect?
Check the User Explorer to see if you can find a specific user who triggered the event. What else did they do? Does it look like a normal users?
You could add a custom dimension of Client ID and parse the GA cookie to track which CIDs are triggering the event (it would help you find them in the User Explorer).
Other custom dimensions that could help you get more context clues: Timestamp, Hit ID (random ID assigned to every individual hit), PHP Referrer.
The long shot: any chance some other script is triggering a click on that button?
I hope something here helps you.

onclick events with Google Tag Manager and Universal Analytics

My client is using Google Tag Manager and Universal Analytics, and would like to be able to track the views for the videos on their website. Their videos are YouTube videos that are displayed in a shadowbox/lightbox via an onclick event.
I added a second onclick event to the same div that opens the shadlowbox, but the datalayer.push events are not appearing in the Analytics reports. I've included my onclick script below:
<div onclick="OpenVideo( 'S6cPlRkiKTw', 'youtube' );dataLayer.push({'eventCategory' : 'videos', 'eventAction' : 'click', 'eventLabel' : ’The Title of the Video', 'event' : 'shadowboxVideo'});”>
The "OpenVideo" function opens the video in the shadowbox and this is working fine, but the clicks are not being tracked in the Analytics reports.
Does anyone see anything wrong with my script or have any other suggestions? Because the client wants to see the title of the video in the reports, I can't think of any other way to do this other than using onclick events, but maybe I'm missing something.
Pushing variables to the datalayer does not by itself send any data to Google Analytics. You need a Google Analytics tag with the type of "Event Tracking" in the tag manager and "event equals shadowboxVideo" as firing rule.
Are there a lot of videos? You could potentially just listen for the element url and then create a rule in UA which replaces a Youtube URL with the title. I have it set up for a client where they just want to know the URL.
Also why not use autoevents in GTM instead of hardcoding?

Hashsignaling and Google Analytics

I am currently working on a site that uses hashsignaling for page loads. This means there are very few page "loads", but rather lots of ajax content changes and template manipulation.
My question is: how do I track this using Google Analytics? Is there a way I can trigger a GA call in my code that captures the entire url including the hash and any other parameters?
It depends how you want to represent your site activity in GA, but you could decide what you want to qualify as a "pageview" and call the _trackPageview() method with URL included as a parameter any time that activity happens:
_gaq.push(['_trackPageview', 'YOUR URL HERE']);
Hashsignaling fire a hashsignal.hashchange event on window object when a page is updated :
$(window).trigger('hashsignal.hashchange', [subhash]);
So you can bind a Google Analytics tracking call to this event with something like :
$(window).on('hashsignal.hashchange', function(event, subhash){
_gaq.push(['_trackPageview', subhash]);
});

Resources