Is there a way to combine loading jQuery and Google Analytics? - javascript

I currently have the following:
<script src="http://www.google.com/jsapi?key=..." type="text/javascript"></script>
<script>
//<![CDATA[
google.load('jquery', '1.6');
//]]>
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '...']);
_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>
Seems like two very different ways to load js. Is there some way I can combine these so the load is most efficient?

For this call you need to load google jsapi first.
google.load('jquery', '1.6');
If you just want the jquery loaded asyncronously parallel to other script file calls, use the following :
var jq = document.createElement(“script”);
jq.src = “/path/to/jquery.js”; jq.type=”text/javascript”; jq.async=true;
document.getElementsByTagName(“head”)[0].appendChild(jq);

Related

Multiple domain tracking with an iframe - landing page showing up as iframe source in GA

So we have website A www.domaina.com and within this website there is an iframe that is shown on all pages, which has source from website B www.domainb.com.
I have setup cross domain tracking using the following code, and if test in GA real time going between the 2 sites, it is correctly showing there is only 1 visitor always. But the landing page (and active page if viewing in Real time) is showing up as the iframe source. I can see this is because using GA debugger, there are multiple tracking beacons being sent out as soon as you land on the home page, and the last one is from the iframe, showing the referring URL being www.domaina.com and the page being domainb.com. THis hence is causing self referrals to show up..
here is the code, but i have no idea how to fix! Im thinking maybe should all the _setDomainName be set to the domain's? I have read conflicting advice on this.
many thanks
GA code domain A
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7338871-1']);
_gaq.push(['_setDomainName', 'none']);
_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>
GA code domain B
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7338871-1']);
_gaq.push(['_setDomainName', 'none']);
_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>
iframe script domain A
<script type="text/javascript">
var iframeLink = "domainb.com";
_gaq.push(function() {
var pageTracker = _gat._getTrackerByName();
iframeLink = pageTracker._getLinkerUrl(iframeLink);
document.getElementById("myIFrame").src = iframeLink;
});
</script>

google analytics code emptying itself

I am trying to implement google analytics to my website. I have pasted their code, which contains such function:
(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 when I upload it to my website it looks like this:
(function() )();
I made it to work (for now) by changeing code to:
<script src="http://www.google-analytics.com/ga.js" async type="text/javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-39971460-1']);
_gaq.push(['_trackPageview']);
</script>
Is it by any chance good solution? Is there any risk with it?
And the actual question - why this function is becoming empty?
Thanks

Two google analytics accounts per page [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
google analytics - multiple trackers on one page (cookie conflict)
I've searched around the internet about this question, but cant find the answer I am looking for.
I've developed a blog, with some special features. Each user get their own subdomain when they register.
Everything is free, but with ads. And I currently use google analytics to track the total number of visitors and pageviews.
I've developed a small counter so that each user can se how many visitors and pageviews they have, but for more detailed informatino they need to use their own "google analytics", or what ever they want to use.
So. The code I use now:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx1-1']);
_gaq.push(['_setDomainName', 'sub.domain.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);
})();
</script>
I dont know what type of analytics service each user will use, I let them insert their own code within the HEAD.
So, IF they use google analytics, will our two accounts at google analytics get the correct number of visitors? The code will then be:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx1-1']);
_gaq.push(['_setDomainName', 'sub.domain.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);
})();
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx2-1']);
_gaq.push(['_setDomainName', 'sub.domain.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);
})();
</script>
Or do I have to do it like:
_gaq.push(
['_setAccount', 'UA-XXXXXXXX-1'],
['_trackPageview'],
['b._setAccount', 'UA-XXXXXXXX-2'],
['b._trackPageview']
);
Where both trackers are within the same . ??
Thanks!
This is an example and it works. I have the same code on mine.
_gaq.push(
['_setAccount', 'UA-XXXXX-1'],
['_trackPageview'],
['b._setAccount', 'UA-XXXXX-2'],
['b._trackPageview']
);

How to use Google Analytics setAllowAnchor flag

I'm trying to get the setAllowAnchor flag in Google Analytics to record campaign information but it doesn't seem to be working.
The site I'm trying to use it on uses hashbang-style URLs BTW - that's the http://hostname/#!/relative-url style of navigation because there needs to be a persistant audio player across page views.
I've tried both of the below, but am not seeing any setAllowAnchor campaign in GA.
/#!/&utm_campaign=setAllowAnchor&utm_medium=hash&utm_source=test
/#utm_campaign=setAllowAnchor&utm_medium=hash&utm_source=test
The function setAllowAnchor is not deprecated. What am I doing wrong?
My Google Analytics code looks like this:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1060190-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_trackPageLoadTime']);
_gaq.push(['_setAllowAnchor', true]);
(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>
Any Google Analytics configuration must come before the _trackPageview or any other _track* functions. So if you rewrite your code to issue the _setAllowAnchor before the _trackPageview it should work.
Also notice that _trackPageLoadTime is not needed anymore. It's issued by default and so ths function is deprecated.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1060190-1']);
_gaq.push(['_setAllowAnchor', 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>

loading google analytics parametrized async page tracking script

If I use the google analytics async site tracking script, in the end of head section of my page, everything works as expected:
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'test.com']);
_gaq.push(['_trackPageview', '/title=ied&action=fire']);
(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>
In the fiddler there can be seen 2 requests:
For some reason I need the script to be parametrized, so I wrap it within custom googleAnalytics function which gets 2 parameters:
function googleAnalytics(domain, queryString) {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', domain]);
_gaq.push(['_trackPageview', queryString]);
(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);
})();
}
Abowe code has been saved to GoogleAnalytics.js file. I loaded it to the page in the head section like below:
<head>
<script src="/script/GoogleAnalytics.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">googleAnalytics('test.com', '/title=ied&action=fire');</script>
</head>
But this time fiddler shows only 1 request:
The Google Analytics Tracking Code Debugger also shows nothing.
Only ga.js script is downloaded but there is no other request which populates data for google analytics report. What is wrong in this approach and how it can be fixed ?
Btw: I need this 2 parameters and the async version of the tracking script.
Regards
Your problem is that the your _gaq declaration is local in scope. ga.js loads, and looks for _gaq in the global scope. (Once the script is injected, it no longer has access to the function's scope.)
To fix it, you can either place var _gaq = _gaq || []; outside of the function, or you can replace it within the function with this:
window._gaq = window._gaq || [];
This will resolve your problem.
I've made this working.
The GoogleAnalytics.js looks following:
function googleAnalytics(domain, queryString) {
_gaq.push(['_setDomainName', domain]);
_gaq.push(['_trackPageview', queryString]);
}
while the head section is as follows:
<head>
<script src="/script/GoogleAnalytics.js" type="text/javascript" language="javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
(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>
<script type="text/javascript">googleAnalytics('com.dev.abb.com','');</script>
</head>
Regards

Categories

Resources