Skip to content

Commit

Permalink
show dom test results
Browse files Browse the repository at this point in the history
  • Loading branch information
dphiffer committed Jul 18, 2024
1 parent 2520947 commit b638de9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 43 deletions.
92 changes: 89 additions & 3 deletions src/DomTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,97 @@ function choose_variant($post) {
$variant['click_content'] = get_field('click_content', $post->ID);
}

// Increment the 'test' variable for this variant.
// $this->plugin->increment('test', $post->ID, $index);

return $variant;
}

/**
* Display the split test results.
*
* @return void
*/
function show_results($post) {
$_variants = get_field('dom_variants', $post->ID);

if (empty($_variants)) {
return;
}

foreach ($_variants as $index => $variant) {
$_variants[$index]['description'] = count($variant['content']) . ' content changes';
}

$variants = [
[
'name' => 'Default',
'description' => 'No content changes'
],
... $_variants
];

?>
<table class="variant-test-results">
<tr>
<th>Variant</th>
<th>Rate</th>
<th>Description</th>
<th>Tests</th>
<th>Conversions</th>
</tr>
<?php foreach ($variants as $index => $variant) {

$num_tests = intval($this->plugin->get_count($post->ID, 'dom', $index, 'test'));
$num_converts = intval($this->plugin->get_count($post->ID, 'dom', $index, 'convert'));
if ($num_tests > 0) {
$rate = number_format($num_converts / $num_tests * 100, 1) . '%';
} else {
$rate = '&mdash;';
}

?>
<tr>
<td><?php echo $variant['name']; ?></td>
<td><?php echo $rate; ?></td>
<td><?php echo $variant['description']; ?></a></td>
<td><?php echo $num_tests; ?></td>
<td><?php echo $num_converts; ?></td>
</tr>
<?php } ?>
</table>
<?php
}

/**
* Show a brief summary of the results, for the split test table column.
*
* @return void
*/
function show_results_summary($post_id) {
$_variants = get_field('dom_variants', $post_id);
$variants = [
['name' => 'Default'],
... $_variants
];
$results = [];
$top_rate = 0;
foreach ($variants as $index => $variant) {
$num_tests = intval($this->plugin->get_count($post_id, 'dom', $index, 'test'));
$num_converts = intval($this->plugin->get_count($post_id, 'dom', $index, 'convert'));
if ($num_tests > 0) {
$rate = $num_converts / $num_tests * 100;
if ($rate > $top_rate) {
$top_rate = $rate;
$top_index = $index;
}
$rate_str = number_format($rate, 1) . '%';
} else {
$rate = 0;
$rate_str = '&mdash;';
}
$results[] = "$rate_str {$variant['name']}";
}
if (isset($top_index)) {
$results[$top_index] = "<strong class=\"split-tests-winner\">$results[$top_index]</strong>";
}
echo implode("<br>\n", $results);
}
}
35 changes: 6 additions & 29 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ function edit_form_after_title($post) {

if ($test_type == 'title') {
$this->title_tests->show_results($post);
} else if ($test_type == 'dom') {
$this->dom_tests->show_results($post);
}
}

Expand Down Expand Up @@ -251,7 +253,7 @@ function wp_enqueue_scripts() {
$nonce = wp_create_nonce('wp_rest');
wp_localize_script('split-tests', 'split_tests', [
'nonce' => $nonce,
'events' => $this->increment_events,
'onload' => $this->increment_events,
'dom' => $this->dom_tests->get_variants()
]);
wp_enqueue_script('split-tests');
Expand Down Expand Up @@ -338,34 +340,9 @@ function manage_split_test_posts_custom_column($column, $post_id) {
}
$type = get_field('test_type', $post_id);
if ($type == 'title') {
$target_id = get_post_meta($post_id, 'target_post_id', true);
$_variants = get_field('title_variants', $target_id);
$variants = [
['name' => 'Default'],
... $_variants
];
$results = [];
$top_rate = 0;
foreach ($variants as $index => $variant) {
$num_tests = intval($this->get_count($post_id, 'title', $index, 'test'));
$num_converts = intval($this->get_count($post_id, 'title', $index, 'convert'));
if ($num_tests > 0) {
$rate = $num_converts / $num_tests * 100;
if ($rate > $top_rate) {
$top_rate = $rate;
$top_index = $index;
}
$rate_str = number_format($rate, 1) . '%';
} else {
$rate = 0;
$rate_str = '&mdash;';
}
$results[] = "$rate_str {$variant['name']}";
}
if (isset($top_index)) {
$results[$top_index] = "<strong class=\"split-tests-winner\">$results[$top_index]</strong>";
}
echo implode("<br>\n", $results);
$this->title_tests->show_results_summary($post_id);
} else if ($type == 'dom') {
$this->dom_tests->show_results_summary($post_id);
}
}

Expand Down
41 changes: 41 additions & 0 deletions src/TitleTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ function increment_convert_count($target_id, $variant_index) {
$this->plugin->increment('convert', $split_test_id, $variant_index);
}

/**
* Display the split test results.
*
* @return void
*/
function show_results($post) {
$target_id = get_post_meta($post->ID, 'target_post_id', true);
$target_post = get_post($target_id);
Expand Down Expand Up @@ -406,4 +411,40 @@ function show_results($post) {
</table>
<?php
}

/**
* Show a brief summary of the results, for the split test table column.
*
* @return void
*/
function show_results_summary($post_id) {
$target_id = get_post_meta($post_id, 'target_post_id', true);
$_variants = get_field('title_variants', $target_id);
$variants = [
['name' => 'Default'],
... $_variants
];
$results = [];
$top_rate = 0;
foreach ($variants as $index => $variant) {
$num_tests = intval($this->plugin->get_count($post_id, 'title', $index, 'test'));
$num_converts = intval($this->plugin->get_count($post_id, 'title', $index, 'convert'));
if ($num_tests > 0) {
$rate = $num_converts / $num_tests * 100;
if ($rate > $top_rate) {
$top_rate = $rate;
$top_index = $index;
}
$rate_str = number_format($rate, 1) . '%';
} else {
$rate = 0;
$rate_str = '&mdash;';
}
$results[] = "$rate_str {$variant['name']}";
}
if (isset($top_index)) {
$results[$top_index] = "<strong class=\"split-tests-winner\">$results[$top_index]</strong>";
}
echo implode("<br>\n", $results);
}
}
23 changes: 12 additions & 11 deletions src/split-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ window.addEventListener('load', () => {
return;
}

if (split_tests.events && split_tests.events.length > 0) {
fetch(`/wp-json/split-tests/v1/events?_wpnonce=${split_tests.nonce}`, {
function postEvents(events) {
if (!events || events.length < 1) {
return;
}
return fetch(`/wp-json/split-tests/v1/events?_wpnonce=${split_tests.nonce}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(split_tests.events)
body: JSON.stringify(events)
});
split_tests.events = [];
}

if (split_tests.onload) {
postEvents(split_tests.onload);
}

if (split_tests.dom) {
for (let id in split_tests.dom) {
const variant = split_tests.dom[id];
postEvents([["test", parseInt(id), variant.index]]);

if (variant.noop) {
continue;
Expand Down Expand Up @@ -44,13 +51,7 @@ window.addEventListener('load', () => {
}
target.addEventListener('click', async e => {
e.preventDefault();
await fetch(`/wp-json/split-tests/v1/events?_wpnonce=${split_tests.nonce}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify([["convert", parseInt(id), variant.index]])
});
await postEvents([["convert", parseInt(id), variant.index]]);
window.location = e.target.getAttribute('href');
});
}
Expand Down

0 comments on commit b638de9

Please sign in to comment.