Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix title conversions #7

Merged
merged 5 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,11 @@ function get_script_details() {
'localize' => [
'endpoint_url' => $endpoint_url,
'nonce' => wp_create_nonce('split_tests_event'),
'tests' => [
... $this->plugin->dom_tests->get_tests(),
... $this->plugin->title_tests->get_tests(),
],
'onload' => $this->plugin->onload_events,
'dom' => $this->plugin->dom_tests->get_variants()
]
];
}
Expand Down
37 changes: 18 additions & 19 deletions src/DomTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ function __construct($plugin) {
}

/**
* Query for DOM variants and select one per test.
* Return any active DOM tests.
*
* @return array
*/
function get_variants() {
$tests = get_posts([
function get_tests() {
$posts = get_posts([
'post_type' => 'split_test',
'meta_key' => 'test_type',
'meta_value' => 'dom'
]);
$variants = [];
foreach ($tests as $post) {
$variant = $this->choose_variant($post);
if (!empty($variant)) {
$variants[$post->ID] = $variant;
$tests = [];
foreach ($posts as $post) {
$test = $this->choose_variant($post);
if (!empty($test)) {
$tests[] = $test;
}
}
return $variants;
return $tests;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these var names are so much clearer

}

/**
Expand All @@ -73,17 +73,16 @@ function choose_variant($post) {
],
... $_variants
];
$index = rand(0, count($variants) - 1);
$variant = $variants[$index];

// Don't include the internal 'name' value.
unset($variant['name']);

// Add the index number
$variant['index'] = $index;

// Add the conversion type
$variant['conversion'] = get_field('conversion', $post->ID);
// Pick a variant at random
$index = rand(0, count($variants) - 1);
unset($variants[$index]['name']); // Don't include the internal 'name' value.
$variant = [
'id' => $post->ID,
'variant' => $index,
'conversion' => get_field('conversion', $post->ID),
... $variants[$index]
];

// Add click conversion details
if ($variant['conversion'] == 'click') {
Expand Down
21 changes: 4 additions & 17 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function deactivate() {
* @return bool
*/
function check_context($test_id) {
if (get_post_status($test_id) != 'publish') {
if (! $test_id || get_post_status($test_id) != 'publish') {
return false;
}
$context = get_field('test_context', $test_id);
Expand Down Expand Up @@ -126,25 +126,12 @@ function check_context_url($test_id) {
}

/**
* Set up a front-end REST API increment call.
* Appends an onload event that will be sent upon the page loading.
*
* @return void
*/
function increment($test_or_convert, $split_test_id, $variant_index) {
$this->onload_events[] = [$test_or_convert, $split_test_id, $variant_index];
}

/**
* Redirect browser to a URL onload.
*
* @return void
*/
function redirect($url) {
if (apply_filters('split_tests_is_headless', false)) {
$this->onload_events[] = ['redirect', $url];
} else {
wp_redirect($url);
}
function add_onload_event() {
$this->onload_events[] = func_get_args();
}

/**
Expand Down
98 changes: 53 additions & 45 deletions src/TitleTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,12 @@ function get_variant($post) {
} else {
$variant_index = 0;
}
if (! $this->converted) {
$this->converted = true;
$variants = get_field('title_variants', $post->ID);
$split_test_post_id = get_post_meta($post->ID, 'split_test_post_id', true);
if ($split_test_post_id && get_post_status($split_test_post_id) != 'publish') {
// If the test is not published, don't convert and redirect
// to the default variant if it's not the selected one.
if ($variant_index > 0) {
$this->redirect_to_default($post->ID);
}
} else if (! empty($variants) && count($variants) > 0) {
$this->increment_convert_count($post->ID, $variant_index);
$test_id = get_post_meta($post->ID, 'split_test_post_id', true);
if ($test_id && get_post_status($test_id) != 'publish') {
// If the test is not published, don't convert and redirect
// to the default variant if it's not the selected one.
if ($variant_index > 0) {
$this->redirect_to_default($post->ID);
}
}
return $variant;
Expand Down Expand Up @@ -211,16 +205,53 @@ function get_variant($post) {
];
$index = rand(0, count($variants) - 1);
$variant = $variants[$index];
$variant['index'] = $index;

// Cache this variant choice.
$this->chosen_variants[$post->ID] = $variant;

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

return $variant;
}

/**
* Returns any active title tests.
*
* @return array
*/
function get_tests() {
$tests = [];
foreach ($this->chosen_variants as $post_id => $variant) {
$test_id = get_post_meta($post_id, 'split_test_post_id', true);
$conversion = get_field('conversion', $test_id);

// If we're on a single page and it's a page load conversion, convert it!
if ($this->is_single() && $conversion == 'page-load') {
$this->plugin->add_onload_event('convert', intval($test_id), $variant['index']);
continue;
}

// If this is the wrong context, skip it.
if (! $this->plugin->check_context($test_id)) {
continue;
}

$test = [
'id' => intval($test_id),
'variant' => $variant['index'],
'conversion' => $conversion,
'noop' => true,
];

if ($conversion == 'click') {
$test['click_content'] = get_field('click_content', $test_id);
$test['click_selector'] = get_field('click_selector', $test_id);
}

$tests[] = $test;
}
return $tests;
}

/**
* Handle 'pre_post_link' filter on post permalinks.
*
Expand Down Expand Up @@ -364,34 +395,6 @@ function get_variant_permalink($split_test_id, $url_slug) {
return str_replace('%postname%', $url_slug, $link_template);
}

/**
* Increment the 'test' statistic.
*
* @return void
*/
function increment_test_count($target_id, $variant_index) {
$split_test_id = get_post_meta($target_id, 'split_test_post_id', true);
if (!$split_test_id) {
return;
}
$split_test_id = intval($split_test_id);
$this->plugin->increment('test', $split_test_id, $variant_index);
}

/**
* Increment the 'convert' statistic.
*
* @return void
*/
function increment_convert_count($target_id, $variant_index) {
$split_test_id = get_post_meta($target_id, 'split_test_post_id', true);
if (!$split_test_id) {
return;
}
$split_test_id = intval($split_test_id);
$this->plugin->increment('convert', $split_test_id, $variant_index);
}

/**
* Redirect to default variant if the split_test post is not published.
*
Expand All @@ -400,7 +403,11 @@ function increment_convert_count($target_id, $variant_index) {
function redirect_to_default($target_id) {
remove_filter('pre_post_link', [$this, 'pre_post_link'], 10, 2);
$default_permalink = get_permalink($target_id);
$this->plugin->redirect($default_permalink);
if (apply_filters('split_tests_is_headless', false)) {
$this->plugin->add_onload_event('redirect', $default_permalink);
} else {
wp_redirect($default_permalink);
}
}

/**
Expand Down Expand Up @@ -500,12 +507,13 @@ function show_results_summary($post_id) {
}

/**
* Returns true if we're loading a single post.
* Returns true if we're on a single post template.
*
* @return bool
*/
function is_single() {
$is_single = (is_single() && get_post_type() == 'post');
return apply_filters('split_tests_is_single', $is_single);
}

}
28 changes: 14 additions & 14 deletions src/split-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ export default function split_tests_init() {
if (split_tests.onload) {
let redirect = split_tests.onload.filter(event => event[0] == 'redirect');
if (redirect.length > 0) {
// This is here to redirect unpublished post title tests back to
// the default permalink.
// This is here to redirect unpublished title tests back to the
// default permalink.
window.location = redirect[0][1];
return;
} else {
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 (split_tests.tests) {
for (let test of split_tests.tests) {

if (variant.content) {
for (let replacement of variant.content) {
postEvents([['test', parseInt(test.id), test.variant]]);

if (test.content) {
for (let replacement of test.content) {
let targets = document.querySelectorAll(replacement.selector);
for (let target of targets) {
if (replacement.search != '' && target.innerText.trim() != replacement.search.trim()) {
Expand All @@ -66,8 +66,8 @@ export default function split_tests_init() {
}
}

if (variant.classes) {
for (let change of variant.classes) {
if (test.classes) {
for (let change of test.classes) {
let targets = document.querySelectorAll(change.selector);
for (let target of targets) {
if (change.change == 'add') {
Expand All @@ -79,17 +79,17 @@ export default function split_tests_init() {
}
}

if (variant.conversion == 'click') {
const selector = variant.click_selector;
const content = variant.click_content;
if (test.conversion == 'click') {
const selector = test.click_selector;
const content = test.click_content;
const targets = document.querySelectorAll(selector);
for (let target of targets) {
if (content && target.innerText.trim() != content.trim()) {
continue;
}
target.addEventListener('click', async e => {
e.preventDefault();
await postEvents([["convert", parseInt(id), variant.index]]);
await postEvents([["convert", parseInt(test.id), test.variant]]);
let href = findHref(e.target);
if (href) {
window.location = href;
Expand Down