Skip to content

Commit

Permalink
allow multiple experiments on the same page
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Jul 11, 2023
1 parent 0c2a01e commit 9070358
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Optimum

## 1.1.0 - 2023-07-11
### Improved
- Allow multiple experiments on the same page

## 1.0.2 - 2022-11-18
### Updated
- Remove delete button from original variant row
Expand Down
23 changes: 12 additions & 11 deletions src/twig/OptimumNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct($nodes, $lineno = 0, $tag = null)
public function compile(Compiler $compiler): void
{
$experiment = $this->getAttribute('experiment')->getAttribute('value');

$e = Experiment::find()->where("handle='$experiment'")->one();

$explicitVariant = $this->getAttribute('variant');
Expand All @@ -48,8 +49,8 @@ public function compile(Compiler $compiler): void
}

$funcs = <<<EOT
if (!function_exists('getOrSetExperimentCookie')):
function getRandomWeightedElement(array \$weightedValues): string
if (!function_exists('getOrSet{$experiment}ExperimentCookie')):
function get{$experiment}RandomWeightedElement(array \$weightedValues): string
{
\$rand = random_int(1, (int)array_sum(\$weightedValues));
Expand All @@ -62,7 +63,7 @@ function getRandomWeightedElement(array \$weightedValues): string
throw new \Exception("Optimum: Failed to randomize element");
}
function getOrSetExperimentCookie(\$experiment, \$vars, \$expiry): string
function getOrSet{$experiment}ExperimentCookie(\$experiment, \$vars, \$expiry): string
{
\$testVar = Craft::\$app->request->getParam('optimum');
Expand All @@ -77,7 +78,7 @@ function getOrSetExperimentCookie(\$experiment, \$vars, \$expiry): string
return \$cookie->value;
}
\$randomVariant = getRandomWeightedElement(\$vars);
\$randomVariant = get{$experiment}RandomWeightedElement(\$vars);
// Create cookie object.
\$cookie = Craft::createObject([
Expand All @@ -95,21 +96,21 @@ function getOrSetExperimentCookie(\$experiment, \$vars, \$expiry): string
endif;
EOT;

$gaevent = 'echo "<script>gtag(\'event\',\'' . $experiment . '\', {\"' . $experiment . '\":\'" . $variantLookup[$variant] . "\'});</script>";';
$gaevent = 'echo "<script>gtag(\'event\',\'' . $experiment . '\', {\"' . $experiment . '\":\'" . $' . $experiment . 'variantLookup[$' . $experiment .'variant] . "\'});</script>";';
$startCondition = (bool)$e->startAt ? " || \Carbon\Carbon::now() < \Carbon\Carbon::parse('$e->startAt')" : "";

$compiler
->addDebugInfo($this);

if (!function_exists('getOrSetExperimentCookie')) {
if (!function_exists("getOrSet{$experiment}ExperimentCookie")) {
$compiler->
raw($funcs);
}

$compiler->raw('$variantLookup =')
$compiler->raw('$' . $experiment . 'variantLookup =')
->repr($varsLookup)
->raw(";\n\n")
->raw('if (!isset($variant)): $variant = getOrSetExperimentCookie("' . $experiment . '",')
->raw('if (!isset($' . $experiment . 'variant)): $' . $experiment . 'variant = getOrSet' . $experiment . 'ExperimentCookie("' . $experiment . '",')
->repr($vars)
->raw(",\Carbon\Carbon::parse('$e->endAt')->unix()); endif;\n\n")
->raw("\$inactive = !$e->enabled || \Carbon\Carbon::now() > \Carbon\Carbon::parse('" . $e->endAt . "') $startCondition; \n\n");
Expand All @@ -121,15 +122,15 @@ function getOrSetExperimentCookie(\$experiment, \$vars, \$expiry): string
}

// Only compile body for original variant when experiment is inactive OR if experiment is active and random variant is the explicit variant
$compiler->raw("if ((!\$inactive && \$variant==='$explicitVariant') || (\$inactive && '$explicitVariant'==='original')):\n\n")
$compiler->raw("if ((!\$inactive && \$" . $experiment ."variant==='$explicitVariant') || (\$inactive && '$explicitVariant'==='original')):\n\n")
->subcompile($this->getNode('body'))
->raw("endif;");
} else {
$compiler->raw("if (!\$inactive): " . $gaevent . " endif;")
->raw("if (\$variant==='original' || \$inactive):\n\n")
->raw("if (\$" . $experiment . "variant==='original' || \$inactive):\n\n")
->subcompile($this->getNode('body'))
->raw("else:")
->raw('$this->loadTemplate("_optimum/' . $experiment . '/{$variant}.twig", null,' . $this->getTemplateLine() . ')->display($context);')
->raw('$this->loadTemplate("_optimum/' . $experiment . '/{$' . $experiment .'variant}.twig", null,' . $this->getTemplateLine() . ')->display($context);')
->raw("endif;");
}
}
Expand Down

0 comments on commit 9070358

Please sign in to comment.