75 lines
2 KiB
PHP
75 lines
2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* A simple class which helps to generate JavaScript snippets which can be
|
|
* embedded in a website to track user interaction with Twingle forms via
|
|
* Matomo.
|
|
*/
|
|
class CRM_TwingleCampaign_Utils_MatomoSnippet {
|
|
|
|
private static function embed_in_base_function($code) {
|
|
return implode("\n",[
|
|
"<!-- matomo -->",
|
|
"<script>",
|
|
"window.addEventListener('message', function(event){",
|
|
"if(event && event.data && event.data.type === 'donationFinished') {",
|
|
$code,
|
|
"}",
|
|
"} , false);",
|
|
"</script>",
|
|
"<!-- matomo -->",
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Returns JavaScript snippet to track events in Matomo.
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function get_event_tracker() {
|
|
$code = "_paq.push(['trackEvent', 'twingle', 'donation', event.data.value.recurringRythm, event.data.value.amount]);";
|
|
return self::embed_in_base_function($code);
|
|
}
|
|
|
|
/**
|
|
* Returns JavaScript snippet to track Matomo goals.
|
|
*
|
|
* @param $goal_id
|
|
* The ID of your Matomo goal.
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function get_goal_tracker($goal_id) {
|
|
$code = "_paq.push(['trackGoal', $goal_id]);";
|
|
return self::embed_in_base_function($code);
|
|
}
|
|
|
|
/**
|
|
* Returns JavaScript snippet to track ecommerce activity in Matomo.
|
|
*
|
|
* @return string
|
|
*/
|
|
public static function get_ecommerce_tracker() {
|
|
$code = implode("\n", [
|
|
"_paq.push(['addEcommerceItem', event.data.value.rythm, '', event.data.value.target, event.data.value.amount]);",
|
|
"_paq.push(['trackEcommerceOrder', 'anonymizedData', event.data.value.amount]);",
|
|
]);
|
|
return self::embed_in_base_function($code);
|
|
}
|
|
|
|
/**
|
|
* Appends the given code to the original code.
|
|
*
|
|
* @param $original
|
|
* The original code.
|
|
* @param $appendix
|
|
* The code you want to append to the original code.
|
|
*
|
|
* @return string
|
|
* The combined code after appending the appendix.
|
|
*/
|
|
public static function append_code($original, $appendix) {
|
|
return $original . '\n' . $appendix;
|
|
}
|
|
|
|
}
|