Skip to content

Instantly share code, notes, and snippets.

@strategyst
Last active September 21, 2016 23:18
Show Gist options
  • Save strategyst/beca68693b193cfbc7caaf5496593643 to your computer and use it in GitHub Desktop.
Save strategyst/beca68693b193cfbc7caaf5496593643 to your computer and use it in GitHub Desktop.
/**
* Add GA Content Experiments Scripts Here
*/
function content_experiments_head_script() {
// Replace '2' with the post/page ID of the original version of the page you are testing
if(is_page(2)) { ?>
<!-- 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>
<script>
// Define JavaScript for each page variation of this experiment.
var pageVariations = [
function() {}, // Original: Do nothing. This will render the default HTML.
function() { // Variation 1: Heading text becomes 'Our New Super Shoe Now Available To Order'
document.getElementById('heading').innerHTML = 'Our New Super Shoe Now Available To Order';
},
function() { // Variation 2: Heading text becomes 'The New Super Shoe - Only £49.99'
document.getElementById('heading').innerHTML = 'The New Super Shoe &mdash; Only &pound;49.99';
},
function() { // Variation 3: Heading text becomes 'The Super Comfy Super Shoe'
document.getElementById('heading').innerHTML = 'The Super Comfy Super Shoe';
},
];
// Wait for the DOM to load, then execute the view for the chosen variation.
jQuery(document).ready(
// Execute the chosen view
pageVariations[chosenVariation]
);
</script>
<?php }
}
add_action( 'wp_head', 'content_experiments_head_script' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment