This is my GA Code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
This code reports to GA Event tracking:
<li>Get Started</li>
But this code doesn't:
<li>Get Started</li>
Any idea what I'm doing wrong?
It's not that the GA tracking isn't firing, but that the data isn't being recorded.
_trackEvent records data by requesting a tracking pixel from analytics servers. Because the link visits a new page right after the _trackEvent call, the tracking pixel request can get canceled, resulting in no data being recorded.
The most common pattern is to add a slight delay (150ms works) before following the link to the new page. Do a search for "_trackEvent delay"
I fixed the issue.
I created this function with a small delay:
<script type="text/javascript">
function trackOutboundLink(link, category, action) {
try {
_gaq.push(['_trackEvent', category , action]);
} catch(err){}
setTimeout(function() {
document.location.href = link.href;
}, 100);
}
</script>
And I call the links like this:
<a href="http://www.example.com" onClick="trackOutboundLink(this, 'Outbound Links', 'example.com'); return false;">
Works like a charm
here your answer from ga team!
I've modified this to get a more easy way:
trackBeforeHref : function(element, category, event, label) {
ga('send', 'event', category, event, label, {'hitCallback':
function () {
document.location = element.href;
}
});
},
then you can use in this way:
<a onclick="trackBeforeHref(this, 'a category', 'a event', 'a label'); return false;"> </a>
Taking advantage of the facts that
_gaq executes the tasks pushed sequentially.
functions can be pushed into _gaq : link to google developers article
so the solution to the problem is
$("a").on("click", function(){
// stop opening the link using return false at end of this event callback function
var href = $(this).attr("href");
// pushing the track event task first and the call to open the href link so that both these tasks happen sequencially.
_gaq.push(['_trackEvent', 'click', 'some_link' ], function(){
window.location = href;
});
return false;
});
Related
on our site we would like to track with Google Analytics user actions in iframe content. The iframes are hosted on a different and 3rd level domain. For some reason my setup doesn't work.
Situation:
In the iframe I want to use two google trackers: one from the parent site, and a separate just for the iframe. I also want to use Events and VPVs. When browsed the page can be seen in GA account's "real-time overview" section. But I don't see a VPV which is attached to Submit button click.
I also don't really understand how function "_setDomainName" works and what should be there. These are separate issues and any help would be much appreciated.
The setup:
On parent site:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-1']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', 'parent.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
$(document).ready(function() {
var pageTracker = _gat._getTrackerByName();
var iframe = document.getElementById('myIFrame');
iframe.src = pageTracker._getLinkerUrl('http://mydomain.iframedomain.net/page.aspx');
});
</script>
ON Iframe page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['a._setAccount', 'UA-XXXXXX-1']);
_gaq.push(['a._setDomainName', 'parent.com']);
_gaq.push(['a._setAllowLinker', true]);
_gaq.push(['a._trackPageview']);
/* this is second tracking account */
_gaq.push(['b._setAccount', 'UA-XXXXXX-2']);
_gaq.push(['b._setDomainName', 'iframedomain.net']);
_gaq.push(['b._trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Then on a button click event I call this function:
function track() {
_gaq.push(['_trackPageview', '/search-retailer-clicked']);
_gaq.push(['_trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
}
what am I doing wrong?
You've named both trackers in the iframe (a._setAccount & b._setAccount), but in the button click event, you're using a default, unnamed tracker.
The outcome is that the virtual page view and event are getting sent to a tracker that hasn't had the account set. If you look at the tracking pixel requests (via ga_debug.js, firebug, chrome developer tools, or fiddler), they'll show a UA-XXXXX-X accountId.
You probably want something more like:
function track() {
_gaq.push(['a._trackPageview', '/search-retailer-clicked']);
_gaq.push(['a._trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
_gaq.push(['b._trackPageview', '/search-retailer-clicked']);
_gaq.push(['b._trackEvent', 'Search - Event', 'Click - Event', 'Search Retailer Button clicked']);
}
The following message appears when viewing a site page in the chrome debug log.
Uncaught ReferenceError: _gaq is not defined
The page itself is supposed to track an object using the onload event handler and fire a _trackEvent for Google Analytics.
My best guess is that perhaps the ga.js file doesn't load in time and therefore the onload _trackEvent triggered is not caught. the async snippet is being used before the </body> close and the object is positioned in the middle <body>.
(some other posts have referenced jQuery position too but, this might be a red herring)
any assistance greatly appreciated.
From https://developers.google.com/analytics/devguides/collection/gajs/ this code replaces your existing "traditional snippet" with the "latest, asynchronous version, you should remove the existing tracking snippet first."
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
To see your events come in (to know if this is working) look for "Events" under the "Real-Time" menu option on the left side of the "Reporting" page.
Regarding the position of the async snippet, the GA help page says -
Paste this snippet into your website template page so that it appears
before the closing </head> tag.
My first thought was that JS should be loaded at the bottom of the page to improve page speed. However, the GA async tracking snippet should be loaded in the head, as it will not load the ga.js immediately, and it won't block page execution.(It does this by dynamically adding the script tag to the DOM, which puts it at the back of the queue.)
If, for some reason, you can't move the async snippet to the head, you can define _gaq yourself, like this-
<button onclick="var _gaq = _gaq || []; _gaq.push(['_trackEvent', 'button3', 'clicked'])"/><button>
Had the same problem. You have to define the _gaq array. Just add this after your Google Analytics script in the header:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
You can use the last version of analytics.js instead of ga.js
ga.js is a legacy library. If you are starting a new implementation we
recommend you use the latest version of this library, analytics.js.
For exisiting implementations, learn how to migrate from ga.js to
analytics.js.
Here is an example:
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'play',
eventLabel: 'cats.mp4'
});
Change the Tracking Code to:
<script type="text/javascript">
var gaq;
var _gaq = gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'yourdomain.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www')
+ '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
I'm trying to use google anayltics events but so far without any success..
What I'm doing is loading 5 pages using jQuery load function
and I want to track the "Next button" for each load. but looks like i'm doing something wrong..
This is the next button event code:
$('.NextButton').click(function () {
_gaq.push(['_trackEvent', 'fz_main_page', 'clicked']);
installation.load_page({ inner_page: next_page, comid: data.comid, outerid: data.outerid, single_app: "1" });
});
Analytics Code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25162785-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
What am I doing wrong?
It's possible that the installation.load_page function is preventing the trackEvent from firing. Try wrapping your load function in a setTimeout:
setTimeout('installation.load_page({ inner_page: next_page, comid: data.comid, outerid: data.outerid, single_app: "1" })', 100);
Install the Google Analytics Debugger. Look in the console (control, shift, j) for the event tracking processing.
If you don't see all of your events tracking there, then something else is up with the tracking code.
Here's the GA output code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA_CODE']);
_gaq.push(['_setDomainName', 'SUBDOMAIN']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackEvent', 'Priority', 'Created (day)', 'Label info', '']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
the trackEvent line seems to be correct...but not tracking for some reason. Is it because I left the value field nil?
For _trackEvent, the value parameter should be an integer. Since it's an optional parameter, you can just leave it out:
_gaq.push(['_trackEvent', 'Priority', 'Created (day)', 'Label info']);
Having a non-integer value will prevent the event from being tracked.
I beleive _trackEvent won't work properly when called from GA tracking code snippet, at least it was not made for such usage. If you want to execute an event after page loads, try calling it for example from onLoad event of your body tag.
Secondly, passing an optional value parametr as '' would make it undefiened, which looks like it could produce an error, so, since it's optional, don't pass it at all.
And there's a delay in GA reportings, data processing takes about 24 hours (you can switch between new and old versions, sometimes one shows data faster than another).
I have been trying to setup a custom variable for the past few days and it hasn't been working.
My Google tracking code is part of a master page (asp.net concept) so I can't set the custom variable inside the second script block labeled "Async Google Code" because it is shared by many other sections.
Below is my code and the order it appears in my page. Is there any way I can set it outside the "Async Google Code" script?
<head>
<!-- Setting Custom Var -->
<script type="text/javascript">
$(function () {
_gaq.push(['_setCustomVar', 1, 'Account', 'UserType', 2]);
}
</script>
<!-- Async Google Code -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
wrap your custom variable stuff in a function to be called and insert a call to that function between the var _gaq... and _trackPageview lines in the bottom piece.
Your "setting custom var" code is missing an end bracket.
You need to set the custom variable before track pageview. You have the custom var in the jQuery document ready shortcut, making it so that it happens after the trackpageview.