• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Better Google Analytics

digitalpoint

Well-known member
#1
Google Analytics allows you to do some interesting things beyond just the default tracking, for example...

  • Site Speed
  • Social Interaction
  • Site Search tracking (what people are searching for ON your site)
You can define your OWN social interactions, so in addition to tracking things like Google+, Likes and Twitter actions, you could even track XenForo Likes if you wanted (or Follows or anything else you deem a social interaction). This is from Google Analytics for digitalpoint.com:





I'd be happy to write the code for XF for it all if developers wanted to use it (for the Facebook/Tweet tracking it's more or less just utilizing the callbacks available in their APIs).

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

This is the Twitter/Facebook tracking we use now (Google+ is automatically tracked already)...

Code:
// Delay binding by 1 second (Twitter doesn't have twttr object yet).
setTimeout(function() {
try {

// Track Facebook Likes, Unlikes and Shares in Google Analytics
FB.Event.subscribe('edge.create', function(targetUrl) {
_gaq.push(['_trackSocial', 'Facebook', 'Like', targetUrl]);
});
FB.Event.subscribe('edge.remove', function(targetUrl) {
_gaq.push(['_trackSocial', 'Facebook', 'Unlike', targetUrl]);
});

/* Not using Send button

FB.Event.subscribe('message.send', function(targetUrl) {
_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
});
*/

// Track Tweets in Google Analytics
twttr.events.bind('tweet', function(event) {
if (event) {
var targetUrl;
if (event.target && event.target.nodeName == 'IFRAME') {
targetUrl = extractParamFromUri(event.target.src, 'url');
}
_gaq.push(['_trackSocial', 'Twitter', 'Tweet', targetUrl]);
}
});
} catch(e) {}
}, 1000);
As a side note, it wouldn't be a bad idea to incorporate the Google Analytics code into a static JS file, where you just pass the profile ID to it. Especially if we start tracking more stuff (no point in bloating pages with a bunch of static analytics tracking code, in my opinion).
 

digitalpoint

Well-known member
#3
Sounds good to me :)
Me too! :p

Oh yeah... forgot the extractParamFromUri() function I use for the tweet tracking...

Edit - is there a secret to making tabs work within CODE BBCode?

Code:
// Used with the Google Analytics Tweet tracking
function extractParamFromUri(uri, paramName) {
if (!uri) {
return;
}
var uri = uri.split('#')[0];  // Remove anchor.
var parts = uri.split('?');  // Check for query params.
if (parts.length == 1) {
return;
}
var query = decodeURI(parts[1]);

// Find url param.
paramName += '=';
var params = query.split('&');
for (var i = 0, param; param = params[i]; ++i) {
if (param.indexOf(paramName) === 0) {
return unescape(param.split('=')[1]);
}
}
}
 

Alfa1

Well-known member
#6
One important aspect of Google analytics is top entry & top exit pages. This identifies threads that are receiving the most traffic and also the threads that are turning away a lot of traffic. If moderators get notified about these threads, then they can optimize those threads and lower the bounce rate, convert more influx into members.
 

digitalpoint

Well-known member
#8
BTW - got around to rewriting this for XF... In case anyone wants to use it..

Obviously _setAccount and _setDomainName should be set to yours (or if it was part of XF default, passed in as a variable).
Code:
// *** Google Analytics ***
	DigitalPoint.Analytics = function() { this.__construct(); };	
	DigitalPoint.Analytics.prototype =
	{
		__construct: function()
		{
			_gaq = [
				['_setAccount', 'UA-12345-1'],
				['_trackPageview'], 
				['_setDomainName', '.yourdomain.com'], 
				['_trackPageLoadTime']
			];

			(function() {
				var ga = document.createElement('script');
				ga.type = 'text/javascript';
				ga.async = true;
				ga.src = '//www.google-analytics.com/ga.js';
				(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
				}
			)();
			
			$(document).ready(function() {
				// Delay binding by 1 second (Twitter doesn't have twttr object yet).
				setTimeout(function() {
					try {
	
	
						// Track Facebook Likes, Unlikes and Shares in Google Analytics
						FB.Event.subscribe('edge.create', function(targetUrl) {
							_gaq.push(['_trackSocial', 'Facebook', 'Like', targetUrl]);
						});
						FB.Event.subscribe('edge.remove', function(targetUrl) {
							_gaq.push(['_trackSocial', 'Facebook', 'Unlike', targetUrl]);
						});

/* Not using Send button

						FB.Event.subscribe('message.send', function(targetUrl) {
							_gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
						});
*/

						// Track Tweets in Google Analytics
						twttr.events.bind('tweet', function(event) {
							if (event) {
								var targetUrl;
								if (event.target && event.target.nodeName == 'IFRAME') {
									targetUrl = DigitalPoint._Analytics.get_param(event.target.src, 'url');
								}
								_gaq.push(['_trackSocial', 'Twitter', 'Tweet', targetUrl]);
							}
						});
					} catch(e) {}
				}, 1000);
			});
		},


		get_param: function (uri, paramName)
		{
			if (!uri) {
				return;
			}
			var uri = uri.split('#')[0];  // Remove anchor.
			var parts = uri.split('?');  // Check for query params.
			if (parts.length == 1) {
				return;
			}
			var query = decodeURI(parts[1]);

			// Find url param.
			paramName += '=';
			var params = query.split('&');
			for (var i = 0, param; param = params[i]; ++i) {
				if (param.indexOf(paramName) === 0) {
					return unescape(param.split('=')[1]);
				}
			}
		}
	};
 

Lone Wolf

Well-known member
#13
BTW - got around to rewriting this for XF... In case anyone wants to use it..

Obviously _setAccount and _setDomainName should be set to yours (or if it was part of XF default, passed in as a variable).
So for example that would be _setXenforo.com for domain but what is the account value?
 

DRE

Well-known member
#15
Can you add this to the Resource Manager please? Kind of don't understand how to add this to my site.