diff --git a/CHANGELOG.md b/CHANGELOG.md index 255677c..914158d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/twig/OptimumNode.php b/src/twig/OptimumNode.php index 01e570d..f809f45 100644 --- a/src/twig/OptimumNode.php +++ b/src/twig/OptimumNode.php @@ -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'); @@ -48,8 +49,8 @@ public function compile(Compiler $compiler): void } $funcs = <<request->getParam('optimum'); @@ -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([ @@ -95,21 +96,21 @@ function getOrSetExperimentCookie(\$experiment, \$vars, \$expiry): string endif; EOT; - $gaevent = 'echo "";'; + $gaevent = 'echo "";'; $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"); @@ -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;"); } }