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.
Related
"_gat is not defined" error on page load
Tried suggestions from: Uncaught ReferenceError: _gaq is not defined (Google Analytics) but didn't help.
Here is my initial code snippet:
(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);
})();
var pageTracker pageTracker = _gat._createTracker('UA-xxxxxxxx-1');
<button onclick="pageTracker._setCustomVar(1,'customvar1', 'value', 3);
pageTracker._trackPageview();">Fire</button>
Here's the solution (adding delay):
(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);
})();
var pageTracker;
setTimeout(function(){
pageTracker = _gat._createTracker('UA-xxxxxxxx-1');
pageTracker._initData();
}, 1000);
<button onclick="pageTracker._setCustomVar(1,'customvar1', 'value', 3);
pageTracker._trackPageview();">Fire</button>
In my tests, I found that 1 second is a good delay period - anything less than 1 second yielded a much higher possibility of getting _gat undefined error.
Old question, I know, but I was able to fix my "_gat is not defined" error by including the first snippet inside the <head> tag. Then including the second snippet anywhere in the body on the page I want to track.
So this goes in the <head:
(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);
})();
And this on any page you want to track:
pageTracker = _gat._createTracker('UA-xxxxxxxx-1');
pageTracker._trackPageview();
I found this js some page sources.actually what does this java script do?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20823326-1']);
_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>
It is the Google Analytics tracker script. You can find out more about it here.
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 this snippet:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-11111111-1']);
_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>
How do I store my account id in web.config? And
how can I get it back from web.config to js (instead of writting the id directly)?
Thanks!
Probably there is no structured way to do it,I just called it from web.config (without using any action, but simply write it) like that:
in web.config:
<appSettings>
<add key="GoogleAnalyticsAccountId" value="UA-11111111-1" />
</appSettings>
in js:
_gaq.push(['_setAccount', '#System.Configuration.ConfigurationManager.AppSettings["GoogleAnalyticsAccountId"]']);
write an ActionResult to return your id
[HttpPost]
public ActionResult GetAnalyticId(){
var id = ConfigutationManager.ApplicationSetting["analyticID"].ToString();
return Content(id);
}
in your javascript
$.post("/Controller/GetAnalyticId",function(data){
//data contains the id use it where you want to
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-11111111-1']);
_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']);