Sharepoint 2010 & Google Analytics

Discovered that you can set a custom variable in Google Analytics to the ID of the Sharepoint user (that’s ID, not Name) :

This is the tracking code I used. The important part here is the _gaq.push([‘_setCustomVar’ line. We can use the Sharepoint JS variable _spUserId:

<script type="text/javascript">
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
  _gaq.push(['_setCustomVar',
		1,         // This custom var is set to slot #1.  Required parameter.
		'User',    // The name of the custom variable.  Required parameter.
		_spUserId, // This is the Sharepoint JS variable _spUserId. Required!
		2          // Sets the scope to session-level.  Optional parameter.
	   ]);
_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>

Don’t forget to change the UA string from

UA-xxxxxxxx-x

to the value for your site!

 Lovely!