As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Could someone please tell me why the analytics code does not load? For US visitors, the alert is being displayed.
<script type="text/javascript">
$.getScript('http://www.geoplugin.net/javascript.gp', function() {
$location = geoplugin_countryName();
if($location == "Canada" || $location == "United States") {
alert("12");
} else {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9240923-27']);
_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>
Just tested this snippet on JSFiddle: http://jsfiddle.net/wDzBc/
$.getScript('http://www.geoplugin.net/javascript.gp', function() {
var location = geoplugin_countryName();
if(location == "Canada" || location == "United States") {
alert("12");
} else {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9240923-27']);
_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('ga');
})();
}
});
If you want analytics to work for Canada and US only, just remove the } else { statement, as mentionned in comments
Edit: ok then:
$.getScript('http://www.geoplugin.net/javascript.gp', function() {
$location = geoplugin_countryName();
if($location == "Canada" || $location == "United States") {
// HERE INSERT THE CODE FOR LOADING ANALYTICS FOR USA
} else {
// HERE INSERT THE CODE FOR LOADING ANALYTICS FOR OTHER COUNTRY
}
});
Since you seem to be wanting to change accounts based on location, the following code should be what you want:
$.getScript('http://www.geoplugin.net/javascript.gp', function() {
var $location = geoplugin_countryName();
var account = "";
if($location == "Canada" || $location == "United States")
account = 'UA-9240923-27'
else
account = "otherAccount";
var _gaq = _gaq || [];
_gaq.push(['_setAccount', account]);
_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);
})();
});
Edit
You have an illegal character at the end of your javascript. This is what it looks like at the end of your script when I view source:
});​
You are also missing the type="text/javascript on your first script tag, and you should properly format your html file with doctype, html, head, and body.
Related
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>
Quick question about the reason why this works:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_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>
And this does not:
<script type="text/javascript">
$(function(){
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_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 is the thing that blocks analytics to run with the document ready?
You need to put _gaq variable declaration to global scope
<script type="text/javascript">
var _gaq = _gaq || [];
$(function(){
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);
...
or make window._gaq property to use with google analytics
<script type="text/javascript">
$(function(){
window.gaq = window._gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
_gaq.push(['_trackPageview']);
...
It likely has to do with the JavaScript code that Google itself is loading into the DOM. Which is likely set to run at document load (or some other readystate/load event). Since the script is being executed when the DOM is ready (jQuery.load) GA won't execute as the ready event already fired.
If that isn't the issue, then it could be a scoping issue.
I've got the google analytics javascript and I want to make it smaller.
But I thought that you couldn't just put an enter somewhere...
So where CAN I start a new line in this code?
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'secret']); _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);
})();
EDIT: Why? Because my screen is to small. It's for readability.
EDIT2: What about this approach? (The use of a '\')
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : '\
http://www') + '.google-analytics.com/ga.js';
I'm really not sure why you want to do this, nor would I recommend it, but here you go.
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'secret']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript'; ga.async = true;
var start;
if ('https:' == document.location.protocol) {
start = 'https://ssl';
} else {
start = 'http://www';
}
ga.src = start + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
try online YUI compressor it works for javascript and css.
You can start a new line after each semi-colon.
Before:
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
After:
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
To my eye, this makes it much easier to read. I wouldn't necessarily keep it this way when I deploy it, but you could.
In addition to being easier to read, this makes it easier to step through line by line if you are debugging.
after any ';'
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'secret']);
_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);
})();
But why do you want to do that?
You can do it manually after every semicolon, but you can also do it automatically. Try entering "javascript formatter" into Google and you get Online JavaScript Beautifier for example.
This is the code after "beautification":
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'secret']);
_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);
})();
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']);
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;
}