Created
July 15, 2014 15:55
-
-
Save osvik/3c62dd9e4f9f561aaf46 to your computer and use it in GitHub Desktop.
Google Analytics AB testing code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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