Custom events not being tracked in Google Analytics - javascript

Here is the script I'm using, copied directly from Google:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-CODE']);
_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);
})();
function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100);
}catch(err){}
}
</script>
And here is the link I'm trying to track:
Yet nothing has shown up in my events report for the last 3 days. Is there something wrong with my code?

There's problems with Google's Outbound links tracking example
Assuming you're only using one tracker (which it looks like in your code), the following will work:
function recordOutboundLink(link, category, action) {
try {
_gaq.push(['_trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100);
}catch(err){}
}

Related

How to fetch and display total number of visits for particular page from google analytics?

I want to show the total number of page visits fetched from google analytics for a url like, https://www.example.in/reviews/company.
How can I display the total count using html, javascript?
I added this code in head tag of the page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_gaq.push(['_initData']);
_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>

SyntaxError: function statement requires a name - jQuery error

What is wrong with my function name?
When I implement the following, I get a SyntaxError: function statement requires a name warning in my console. I have included jQuery earlier in the code, so it's not that.
function($) {
$(document).ready(function() {
$(':input').blur(function () {
if($(this).val().length > 0) {
_gaq.push(['_trackEvent', 'Modals', 'completed', $(this).attr('name')]);
}
else {
_gaq.push(['_trackEvent', 'Modals', 'skipped', $(this).attr('name')]);
}
});
});
})(jQuery);
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '*****']);
_gaq.push(['_setDomainName', '*****']);
_gaq.push(['_trackPageview'], ['_trackPageLoadTime']);
(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);
})();
Can anyone help?
you are missing a bracket in line one, missing '('

Google Analytics javascript

Here is a Google Analytics' code
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20366831-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);
})();
How my client side calls Google anonymous function?
It's called because the anonymous function ends with ()
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})(); // <--- The () calls the anonymous code
As you'll see, this code basically injects a script tag into the DOM, which gets run by the browser.
That snippet already call itself.
(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);
})();
what it actually does is including the ga.js on your page, which is similar to this:
<script src="//google-analytics.com/ga.js" />
The rest is up to you to add event to the _gaq (google analytic queue). Then the event will automatically be processed.

Google Analytics : _setvar to new tracking code

I have been using the older version of analytics code and used the following to track different types of users
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-xxxxxxx");
pageTracker._setVar('memberlevel-2'); pageTracker._trackPageview();
} catch(err) {}</script>
How do I use this with the new asynchronous code? Google Analytics forums is dead and i got no response :(
Try reading this for setting custom variable:
http://code.google.com/apis/analytics/docs/tracking/gaTrackingCustomVariables.html
Sample code for to track page view:
<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>
_setVar still works, but you should use _setCustomVar instead, as its more powerful.
Here's what your old code looks like using the async code:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setVar', 'memberlevel-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);
})();
To use setCustomVar instead, you could do this:
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setCustomVar', 1, 'memberlevel', '2', 3]);// page-level scope (3), in slot #1
_gaq.push(['_trackPageview']);

Conditions syntax help

I'm trying to include this Regex condition and activate the analytics code (if it meets the Regex condition) in a javascript function posted below.
if (/^[abc].*/i.test(mystring)) {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-XX']);
_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);
})();
alert('Congrats ABC!');
}
Now how can I add the code above (regex condition and the analytics) in there?Also, please note. The portion below works perfectly at the moment.
function validateCoupon( form ){
var mystring = document.getElementById('textCoupon').value;
if( form.textCoupon.value.length )
{
// validate coupon
$.get( "/include/checkItOut.php", { coupon: form.textCoupon.value }, validateShipping );
alert('Performed the check and matched');
}
else
{
form.textCoupon.style.border = '';
validateShipping( "yes" );
alert('You Did Not Use Any Code');
}
return false;
}
In other words. Somehow include the Regex condition and analytics trigger to this function here.
A literal comparison between the coupon code (in lower case) and abc using the equality operator (==) is enough. You should consider a server side solution, everyone can open the source and fetch the coupon code "abc" from it.
function validateCoupon( form ){
var mystring = document.getElementById('textCoupon').value;
if( form.textCoupon.value.length )
{
// added: Analytics
if ( form.textCoupon.value.toLowerCase() == "abc") {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-XX']);
_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);
})();
}
// end analytics
// validate coupon
$.get( "/include/checkItOut.php", { coupon: form.textCoupon.value }, validateShipping );
alert('Performed the check and matched');
}
else
{
form.textCoupon.style.border = '';
validateShipping( "yes" );
alert('You Did Not Use Any Code');
}
return false;
}

Categories