PDA

View Full Version : Google Analytics for Ajax Tabs Content script



developer
12-03-2009, 08:25 AM
1) Script Title: Ajax Tabs Content script v2.2

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex17/ajaxtabscontent/
http://www.dynamicdrive.com/dynamicindex17/ajaxtabscontent/ajaxtabs/ajaxtabs.js

3) Describe problem: The Google Analytics does not record ajaxtabscontent activity.
Does someone knows to change ajaxtabs.js to active it for Google Analytics records?
Do I need to change also Google Analytics supported js code?

jscheuer1
12-03-2009, 10:02 AM
I'd try - Let's say your tabs instance is countries, ex:


var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
countries.setpersist(true)
countries.setselectedClassTarget("link") //"link" or "linkparent"
countries.init()

Add this code right below that:


countries.onajaxpageload = function(){
pageTracker._initData();
pageTracker._trackPageview();
};

developer
12-03-2009, 04:48 PM
Then probably it must look:

countries.onajaxpageload=function(pageurl){
pageTracker._initData();
pageTracker._trackPageview();}

http://www.dynamicdrive.com/dynamicindex17/ajaxtabscontent/ajaxtabs_suppliment2.htm

jscheuer1
12-03-2009, 06:34 PM
The page's URL parameter is passed to the onajaxpageload function regardless of whether or not it is specified as the function sole argument, which is what it is. If not used by the code in the onajaxpageload function it need not be given a token, which is only required for convenience if you are planning on using it. It will always be available within the onajaxpageload function as:


arguments[0]

But since it isn't required for:


pageTracker._initData();
pageTracker._trackPageview();

to do its work, it may be omitted.

The only possible problem is if:


pageTracker._initData();
pageTracker._trackPageview();

doesn't properly initialize the newly imported content for some reason. However, it is very likely that it will.

developer
12-09-2009, 05:33 PM
Thanks jscheuer1 for advice, but code does not work for me. The Google Analytics does not record ajaxtabscontent activity.

Before </head> I have:
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));my axtabscontent:
var countries=new ddajaxtabs("countrytabs", "countrydivcontainer")
countries.setpersist(true)
countries.setselectedClassTarget("link")
countries.init()
countries.onajaxpageload=function(){
pageTracker._initData();
pageTracker._trackPageview();
}
countries.onajaxpageload=function(pageurl){
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox()
})
pageTracker._initData();
pageTracker._trackPageview();
}and before </body>
try{
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}

Do you have some advice? Maybe my JavaScript has some mistakes?

developer
12-11-2009, 10:56 AM
some solution?

jscheuer1
12-11-2009, 12:10 PM
Sorry, I had lost track of this thread. Yes, your code is at least odd, perhaps ultimately doing nothing on tab load. You may only have one countries.onajaxpageload function, in your case, the second one overwrites the first, so:


countries.onajaxpageload=function(){
pageTracker._initData();
pageTracker._trackPageview();
}

never happens. This one:


countries.onajaxpageload=function(pageurl){
jQuery(document).ready(function($) {
$('a[rel*=facebox]').facebox()
})
pageTracker._initData();
pageTracker._trackPageview();
}

overwrites it. But the jQuery(document).ready event has already passed, so I doubt that part does anything, it may even throw an error and prevent execution of:


pageTracker._initData();
pageTracker._trackPageview();

So, I'd just forget about facebox, at least until we can see if we can get the Google Analytics working. I took my init for that from one I found on a page on Dynamic Drive, but yours looks different, so let's try that. Replace both of your countries.onajaxpageload functions with:


countries.onajaxpageload=function(){
try{
var pageTracker = _gat._getTracker("UA-XXXXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}
};

Make sure the live page is updated and not showing in the browser. Clear the browser's cache, then navigate to the live page to see what the effect has been.

We still have a potential problem, one I'm not sure if there is anything we can do about. Either or both of these functions:

_gat._getTracker("UA-XXXXXXXX-X")

and:

instance._trackPageview()

May only be allowed to run once. If it's just _gat._getTracker("UA-XXXXXXXX-X") we could remove that line and try again with just:


countries.onajaxpageload=function(){
try{
pageTracker._trackPageview();
} catch(err) {}
};

If the instance._trackPageview() can only be run once, there isn't a lot we can do. Perhaps there is a forum where Google Analytics is discussed. If so, they may have a solution.

Another thing to try though would be using only:


countries.onajaxpageload=function(){
try{
pageTracker._initData();
pageTracker._trackPageview();
} catch(err) {}
};

But it could be possible that the Google Analytics code is not flexible enough to be applied to AJAX imported content.

jscheuer1
12-11-2009, 12:27 PM
Hmm, I'm looking into this more. And now I see that we should be able to track any event:

http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html

But what is it that you want to track? Do you want to track when a tab is clicked? Or do you want to track when things happen inside the imported content?

developer
01-23-2010, 09:47 PM
Do you want to track when a tab is clicked? Or do you want to track when things happen inside the imported content?

I would like to track ajax content loading requests.
jscheuer1, do you have some solution?

jscheuer1
01-24-2010, 01:35 AM
I think I know what you are getting at, but after so much time it will take some research of the link from my last post. So, just to be clear, as your answer was a little ambiguous:


Do you want to track when a tab is clicked?

OR:


Do you want to track the clicks that occur within the content that is imported when a tab is clicked after said content has been imported?


Please respond with either the number 1 or the number 2, or with a detailed explanation of what you want to track (that isn't already being tracked) if neither of the above represents it correctly. I think you are going for number 2. But I'm not 100% sure yet. Let me know.

developer
01-24-2010, 08:06 PM
I think I know what you are getting at, but after so much time it will take some research of the link from my last post. So, just to be clear, as your answer was a little ambiguous:


Do you want to track when a tab is clicked?

OR:


Do you want to track the clicks that occur within the content that is imported when a tab is clicked after said content has been imported?


Please respond with either the number 1 or the number 2, or with a detailed explanation of what you want to track (that isn't already being tracked) if neither of the above represents it correctly. I think you are going for number 2. But I'm not 100% sure yet. Let me know.

Yes, jscheuer1, my question is about number 2.