Google Analytics inside Document ready not working? - javascript

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.

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>

Entourage.js not working

I have added entourage.js on a website to automatically track any file downloads like .doc,.pdf files etc but it does not seem to track any thing. I have the following code in the head section of the website
<script src="/scripts/entourage.js" type="text/javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-xx']);
_gaq.push(['_trackPageview']);
</script>
<script type="text/javascript">
(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 ideas why

javascript process explain

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.

Store Account Id for Google Analytics in Web.config (MVC 3)

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);
})();
});

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']);

Categories