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

Better Google Analytics

Discussion in 'XenForo Suggestions' started by digitalpoint, Oct 16, 2011.

  1. digitalpoint

    digitalpoint Well-Known Member

    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:

    [​IMG]
    [​IMG]
    [​IMG]

    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).
     
  2. Kier

    Kier XenForo Developer Staff Member

    Sounds good to me :)
     
  3. digitalpoint

    digitalpoint Well-Known Member

    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]);
    }
    }
    }
     
  4. Kier

    Kier XenForo Developer Staff Member

    F*&@£(*&@(&! WYSIWYG editor...

    Yeah, I switch to the plain <textarea> editor before pasting (top right gadget on the editor toolbar), and then keep it in that mode until the message is posted.
     
    The8thLegion likes this.
  5. Walter

    Walter Well-Known Member

    In addition to Google Analytics, please also support the open alternative Piwik.
     
    rellek and Deebs like this.
  6. Alfa1

    Alfa1 Well-Known Member

    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.
     
  7. Hoffi

    Hoffi Well-Known Member

    I'd like to see somthing similar to Piwik. Google Analytics is blocked by many users.
     
  8. digitalpoint

    digitalpoint Well-Known Member

    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]);
    				}
    			}
    		}
    	};
     
  9. MGSteve

    MGSteve Well-Known Member

    Surely they'll just block Piwik if they're that way inclined anyway?
     
  10. Hoffi

    Hoffi Well-Known Member

    Not all. Mostly they don't know Piwik and if its stored on my domain... ;)
     
  11. Carlos

    Carlos Well-Known Member

    If Kier decides to make this a feature, I ask that this be a toggle.
     
  12. akia

    akia Well-Known Member

    Do you just swap the standard code for this in the template for it to work
     
  13. Lone Wolf

    Lone Wolf Well-Known Member

    So for example that would be _setXenforo.com for domain but what is the account value?
     
  14. digitalpoint

    digitalpoint Well-Known Member

    Your Analytics account ID.
     
  15. DRE

    DRE Well-Known Member

    Can you add this to the Resource Manager please? Kind of don't understand how to add this to my site.
     
  16. Chatwic

    Chatwic New Member

    Where do you input this code to work with your XF?
     
  17. digitalpoint

    digitalpoint Well-Known Member

    Finally did... wasn't possible without the Template Modifiation system in 1.2... so if you are running 1.2 or higher, it should work:

    http://xenforo.com/community/resources/better-analytics.2288/
     
    QenTox and DRE like this.
  18. DRE

    DRE Well-Known Member

  19. digitalpoint

    digitalpoint Well-Known Member

    Nope, that's something totally different.
     
  20. Maestro2k5

    Maestro2k5 Member

    Where can I integrate this code with?

    _gaq.push(['_gat._anonymizeIp']);
     

Share This Page