This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Headline testing via Experiment API #1
Merged
+544
−66
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
83c9cb1
Add box in admin view for listing alternate headlines
0f856ee
Add code for creating and applying variations (content.php)
8e7c4a5
Get API credentials in plugin settings page + check for them
17dac9b
Refactored admin.php into admin + config.php
thatsjonsense 8e42d89
Use new authentication (incomplete)
thatsjonsense e725bc5
API connection working!
16f1c45
Variation template
e2e23f4
Missing )
ef9946e
PHP API binding
1eed52e
Choose project from drop down + generate snippet!
45507e9
All the API calls...except they don't work :/
thatsjonsense 27dc384
JS API connection implemented
fe5003c
THE PLUGIN WORKS OMGOMGOMG
30a79b1
Separated create/edit experiment
6a8dc43
Lots of refactoring, new auth
1493644
Code cleanup + save data async
059d417
Goal creation (in progress)
ca989ca
Goals working
af4f9b4
Added inline comments/explanations + turned num_variations into a con…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
function optimizelyConfigPage() { | ||
var $ = jQuery; | ||
|
||
// Select a project ID | ||
$('#project_id').change(function() { | ||
var id = $('#project_id').val(); | ||
var name = $('#project_id option:selected').text(); | ||
var project_code = '<script src="//cdn.optimizely.com/js/' + id + '.js"></script>'; | ||
$('#project_code').text(project_code); | ||
$('#project_name').val(name); | ||
}); | ||
|
||
// Get list of projects | ||
$("button#connect_optimizely").click(function(event) { | ||
event.preventDefault(); | ||
$("#project_id").html("<option>Loading projects...</option>"); | ||
|
||
optly = new OptimizelyAPI($("#token").val()); | ||
|
||
optly.get('projects', function(response) { | ||
$("#project_id").empty(); | ||
|
||
$.each(response, function(key, val) { | ||
$("#project_id").append("<option value='" + val.id + "'>" + val.project_name + "</option>"); | ||
}); | ||
|
||
$("#project_id").change(); | ||
}); | ||
|
||
|
||
|
||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<div class="wrap"> | ||
<h2><?php _e('Optimizely Configuration'); ?></h2> | ||
<div class="narrow"> | ||
<form action="" method="post" id="optimizely-conf"> | ||
<?php optimizely_nonce_field($optimizely_nonce) ?> | ||
<h3>About Optimizely</h3> | ||
<p>Simple, fast, and powerful. <a href="http://www.optimizely.com" target="_blank">Optimizely</a> is a dramatically easier way for you to improve your website through A/B testing. Create an experiment in minutes with absolutely no coding or engineering required. Convert your website visitors into customers and earn more revenue: create an account at <a href="http://www.optimizely.com" target="_blank">optimizely.com</a> and start A/B testing today!</p> | ||
<h3>Optimizely API tokens</h3> | ||
<p>Once you create an account, you can find your API Token on your account page at <a href="https://www.optimizely.com/account">optimizely.com/account</a>. | ||
<p> | ||
<label for="token"><strong>API Token</strong></label> | ||
<br /> | ||
<input id="token" name="token" type="text" maxlength="80" value="<?= get_option('optimizely_token'); ?>" class="code" /> | ||
</p> | ||
|
||
<button id="connect_optimizely" class="button">Connect Optimizely</button> | ||
|
||
<h3>Choose a Project</h3> | ||
<input type="hidden" id="project_name" name="project_name" value="<?= get_option('optimizely_project_name') ?>" /> | ||
<select id="project_id" name="project_id"> | ||
<?php if (get_option('optimizely_project_id')) { ?> | ||
<option value="<?= get_option('optimizely_project_id') ?>" selected><?= get_option('optimizely_project_name') ?></option> | ||
<?php } ?> | ||
<option value="">Connect Optimizely to choose a project...</option> | ||
</select> | ||
<p>Optimizely will add the following project code to your page automatically:</p> | ||
<textarea class="code" id="project_code" name="project_code" readonly><?= get_option('optimizely_project_code') ?></textarea> | ||
|
||
|
||
<h3>Variation Code</h3> | ||
<p>Optimizely will use this variation code to change headlines on your site. We've provided code that works with the default theme, but you might want to add or change it to work with your themes and plugins.</p> | ||
|
||
<textarea class="code" rows="5" name="variation_template" id="variation_template"><?= get_option('optimizely_variation_template') ?></textarea> | ||
|
||
<p>You can use the variables $POST_ID, $OLD_TITLE, and $NEW_TITLE in your code.</p> | ||
|
||
<p class="submit"><input type="submit" name="submit" value="<?php _e('Submit »'); ?>" class="button-primary" /></p> | ||
|
||
|
||
</form> | ||
<script type="text/javascript"> | ||
optimizelyConfigPage(); | ||
</script> | ||
|
||
|
||
|
||
</div> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
function optimizelyEditPage() { | ||
var $ = jQuery; | ||
|
||
// Initialize data | ||
var projectId = $('#optimizely_project_id').val(); | ||
var optly = new OptimizelyAPI($("#optimizely_token").val()); | ||
optly.experiment = { | ||
id: $("#optimizely_experiment_id").val(), | ||
status: $("#optimizely_experiment_status").val() | ||
} | ||
|
||
if (optly.experiment.id) { | ||
showExperiment(optly.experiment); | ||
} else { | ||
$('#optimizely_created').hide(); | ||
} | ||
|
||
$('#optimizely_create').click(function(){ | ||
createExperiment(); | ||
}); | ||
|
||
$('#optimizely_toggle_running').click(function(){ | ||
if (optly.experiment.status == "Running") { | ||
pauseExperiment(optly.experiment); | ||
} else { | ||
startExperiment(optly.experiment); | ||
} | ||
}); | ||
|
||
// Render the experiment's state on the page | ||
function showExperiment(experiment) { | ||
// ID and links | ||
$("#optimizely_experiment_id").val(experiment.id); | ||
$('#optimizely_view').attr('href','https://www.optimizely.com/edit?experiment_id=' + experiment.id); | ||
$('#optimizely_results').attr('href','https://www.optimizely.com/results?experiment_id=' + experiment.id); | ||
|
||
// Status and buttons | ||
$("#optimizely_experiment_status").val(experiment.status); | ||
$('#optimizely_experiment_status_text').text(experiment.status); | ||
if (experiment.status == "Running") { | ||
$('#optimizely_toggle_running').text('Pause Experiment'); | ||
} else { | ||
$('#optimizely_toggle_running').text('Start Experiment'); | ||
} | ||
|
||
// Hide create button, show status | ||
$('#optimizely_not_created').hide(); | ||
$('#optimizely_created').show(); | ||
|
||
// Update Wordpress backend w/ experiment data | ||
var data = { | ||
action: "update_experiment_meta", | ||
post_id: $('#post_ID').val(), | ||
optimizely_experiment_id: experiment.id, | ||
optimizely_experiment_status: experiment.status | ||
}; | ||
|
||
$('.optimizely_variation').each(function(index, input) { | ||
data[$(input).attr('name')] = $(input).val(); | ||
}); | ||
$.post(wpAjaxUrl, data); | ||
} | ||
|
||
function createExperiment() { | ||
$('#optimizely_create').text('Creating...'); | ||
|
||
experiment = {}; | ||
experiment.description = "Wordpress: " + $('#title').val(); | ||
experiment.edit_url = $('#sample-permalink').text(); | ||
|
||
optly.post('projects/' + projectId + '/experiments', experiment, onExperimentCreated); | ||
} | ||
|
||
function onExperimentCreated(experiment) { | ||
|
||
optly.experiment = experiment; | ||
|
||
var variations = $('.optimizely_variation').filter(function(){return $(this).val().length > 0}) | ||
|
||
// Set variation weights | ||
var numVariations = variations.length + 1; | ||
var variationWeight = Math.floor(10000 / numVariations); | ||
var leftoverWeight = 10000 - variationWeight*numVariations; | ||
|
||
// Create variations | ||
variations.each(function(index, input) { | ||
var weight = variationWeight + (index == 0 ? leftoverWeight : 0); | ||
createVariation(experiment, index + 1, $(input).val(), weight); | ||
}); | ||
|
||
// Create goal | ||
createGoal(experiment); | ||
|
||
} | ||
|
||
function createGoal(experiment) { | ||
|
||
var goal = { | ||
goal_type: 3, // pageview goal | ||
title: "Views to page", | ||
urls: [$('#sample-permalink').text()], | ||
url_match_types: [4], // substring | ||
// addable: false, // don't clog up the goal list | ||
experiment_ids: [experiment.id] | ||
} | ||
|
||
optly.post('projects/' + experiment.project_id + '/goals/', goal, checkExperimentReady); | ||
|
||
} | ||
|
||
function createVariation(experiment, index, newTitle, weight) { | ||
|
||
// Generate variation code | ||
var variationTemplate = $('#optimizely_variation_template').val(); | ||
var postId = $('#post_ID').val(); | ||
var originalTitle = $('#title').val(); | ||
var code = variationTemplate | ||
.replace(/\$OLD_TITLE/g, originalTitle) | ||
.replace(/\$NEW_TITLE/g, newTitle) | ||
.replace(/\$POST_ID/g, postId); | ||
|
||
// Request data | ||
var variation = { | ||
"description": newTitle, | ||
"js_component": code, | ||
"weight": weight, | ||
} | ||
|
||
// Update variation #1, create the others | ||
if (index == 1) { | ||
optly.patch('variations/' + experiment.variation_ids[1], variation, checkExperimentReady); | ||
} else { | ||
optly.post('experiments/' + experiment.id + '/variations', variation, checkExperimentReady); | ||
} | ||
|
||
} | ||
|
||
function checkExperimentReady(response) { | ||
if (optly.outstandingRequests == 0) { | ||
showExperiment(optly.experiment); | ||
} | ||
} | ||
|
||
function startExperiment(experiment) { | ||
$('#optimizely_toggle_running').text('Starting...'); | ||
optly.patch('experiments/' + experiment.id, {'status': 'Running'}, function(response) { | ||
optly.experiment = response; | ||
showExperiment(optly.experiment); | ||
}); | ||
} | ||
|
||
function pauseExperiment(experiment) { | ||
$('#optimizely_toggle_running').text('Pausing...'); | ||
optly.patch('experiments/' + experiment.id, {'status': 'Paused'}, function(response) { | ||
optly.experiment = response; | ||
showExperiment(optly.experiment); | ||
}); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just as a head's up, "empty" in PHP is rather inclusive. The following are considered empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
Just makes sure this jibes with what you want to do here.