Skip to content

Commit

Permalink
add setting
Browse files Browse the repository at this point in the history
  • Loading branch information
matfish3 committed Sep 21, 2024
1 parent 41dd270 commit e0087a6
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/services/trackingcode/GA4TrackingCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace matfish\Optimum\services\trackingcode;

use matfish\Optimum\records\Experiment;
use matfish\Optimum\records\Variant;

class GA4TrackingCode implements TrackingCode
{
/**
* Generate the JavaScript tracking event code for GA4 using a Custom Dimension.
*
* @param Experiment $experiment
* @param Variant $variant
* @return string
*/
public function generate(Experiment $experiment, Variant $variant): string
{
return <<<EOD
gtag('event','$experiment->handle', {'$experiment->handle':'$variant->name'});
EOD;
}
}
27 changes: 27 additions & 0 deletions src/services/trackingcode/MixpanelTrackingCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace matfish\Optimum\services\trackingcode;

use matfish\Optimum\records\Experiment;
use matfish\Optimum\records\Variant;

class MixpanelTrackingCode implements TrackingCode
{
/**
* Generate the JavaScript tracking event code for Mixpanel.
*
* @param Experiment $experiment
* @param Variant $variant
* @return string
*/
public function generate(Experiment $experiment, Variant $variant): string
{
return <<<EOD
mixpanel.track('\$experiment_started', {
'Experiment Name': '$experiment->handle',
'Variant Name': '$variant->name'
});
EOD;
}
}

20 changes: 20 additions & 0 deletions src/services/trackingcode/TrackingCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace matfish\Optimum\services\trackingcode;


use matfish\Optimum\records\Experiment;
use matfish\Optimum\records\Variant;

interface TrackingCode
{
/**
* Generate the JavaScript tracking event code.
*
* @param Experiment $experiment
* @param Variant $variant
* @return string
*/
public function generate(Experiment $experiment, Variant $variant): string;
}

41 changes: 41 additions & 0 deletions src/services/trackingcode/TrackingCodeRetriever.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace matfish\Optimum\services\trackingcode;

use matfish\Optimum\Plugin as Optimum;
use matfish\Optimum\records\Experiment;
use matfish\Optimum\records\Variant;

class TrackingCodeRetriever
{
/**
* Retrieve the JavaScript tracking code.
*
* @param Experiment $experiment
* @param Variant $variant
* @return string
*/
public function getTrackingCode(Experiment $experiment, Variant $variant): string
{
$settings = Optimum::getInstance()->getSettings();

if ($settings->fireEvent !== null) {
return ($settings->fireEvent)($experiment, $variant);
}

$trackingPlatform = $settings->trackingPlatform;

switch ($trackingPlatform) {
case 'ga4':
$trackingCode = new GA4TrackingCode();
break;
case 'mixpanel':
$trackingCode = new MixpanelTrackingCode();
break;
default:
throw new \InvalidArgumentException("Unsupported tracking platform: $trackingPlatform. Use the `fireEvent` config setting to specify a custom function.");
}

return $trackingCode->generate($experiment, $variant);
}
}

0 comments on commit e0087a6

Please sign in to comment.