-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |