Skip to content

Instantly share code, notes, and snippets.

@osvik
Created July 15, 2014 15:55
Show Gist options
  • Save osvik/3c62dd9e4f9f561aaf46 to your computer and use it in GitHub Desktop.
Save osvik/3c62dd9e4f9f561aaf46 to your computer and use it in GitHub Desktop.
Google Analytics AB testing code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AB Google Analytics testing code</title>
<!-- Load the Content Experiment JavaScript API client for the experiment -->
<script src="//www.google-analytics.com/cx/api.js?experiment=[YOUR EXPERIMENT ID]"></script>
<script>
// Ask Google Analytics which variation to show the user.
var chosenVariation = cxApi.chooseVariation();
</script>
<!-- Load Jquery -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<script>
// Define JavaScript for each page variation of this experiment.
var pageVariations = [
function() { // Original: Do nothing. This will render the default HTML.
alert("Original");
},
function() { // VARIATION 1: Banner Image
alert("Variation 1");
// document.getElementById('banner').src = 'bay-bridge.jpg';
},
function() { // VARIATION 2: Sub-heading Text
alert("Variation 2");
// document.getElementById('heading').innerHTML = 'Look, a Bridge!';
},
function() { // VARIATION 3: Button Text
alert("Variation 3");
// document.getElementById('button').innerHTML = 'Learn more';
},
function() { // VARIATION 4: Button Color
alert("Variation 4");
// document.getElementById('button').className = 'button button-blue';
}
];
// Wait for the DOM to load, then execute the view for the chosen variation.
$(document).ready( function() {
pageVariations[chosenVariation]();
});
</script>
<script>
// Add Google Analytics tracking code and a Google Analytics goal (can be an event)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment