labs-scnm
6/12/2012 - 10:53 AM

Google Analytics Tracking-Code zum Protokollieren von simplen A/B-Tests

Google Analytics Tracking-Code zum Protokollieren von simplen A/B-Tests

<script type="text/javascript">
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-XXXXX-Y']);
	_gaq.push(['_trackPageview']);
	
	_gaq.push(
		function() {
			var tracker = _gat._getTrackerByName(),
				group = tracker._getVisitorCustomVar(1),
				random = Math.random();
			
			if ( typeof(group) === 'undefined' ) {
				if ( random < 0.25 ) {
					group = 'A';
				} else if ( random < 0.50 ) {
					group = 'B';
				} else if ( random < 0.75 ) {
					group = 'C';
				} else {
					group = 'D';
				}
				
				_gaq.push(
					['_setCustomVar', 1, 'A/B Test', group, 1]
				);
			}
			
			switch(group) {
				case 'A':
					/* Handlung für Version A */
				break;
				
				case 'B':
					/* Handlung für Version B */
				break;
				
				case 'C':
					/* Handlung für Version C */
				break;
				
				default:
					/* Handlung für Version D */
				break;
			}
		}
	);
	
	(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>