implement matomo integration
This commit is contained in:
parent
2949ab0168
commit
82b4632d63
5 changed files with 134 additions and 6 deletions
|
@ -7,7 +7,8 @@ class CRM_TwingleCampaign_BAO_Configuration {
|
||||||
'twingle_api_key',
|
'twingle_api_key',
|
||||||
'twinglecampaign_xcm_profile',
|
'twinglecampaign_xcm_profile',
|
||||||
'twinglecampaign_default_case',
|
'twinglecampaign_default_case',
|
||||||
'twinglecampaign_soft_credits'
|
'twinglecampaign_soft_credits',
|
||||||
|
'twinglecampaign_matomo_integration'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
@ -27,6 +28,12 @@ class CRM_TwingleCampaign_BAO_Configuration {
|
||||||
Civi::settings()->set('twinglecampaign_soft_credits', 0);
|
Civi::settings()->set('twinglecampaign_soft_credits', 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set twinglecampaign_matomo_integration to '0' if checkbox is unchecked
|
||||||
|
if (!array_key_exists('twinglecampaign_matomo_integration', $settings)) {
|
||||||
|
Civi::settings()->set('twinglecampaign_matomo_integration', 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Civi::settings()->add($settings);
|
Civi::settings()->add($settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,13 @@ class CRM_TwingleCampaign_Form_Settings extends CRM_Core_Form {
|
||||||
FALSE
|
FALSE
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->addElement(
|
||||||
|
'checkbox',
|
||||||
|
'twinglecampaign_matomo_integration',
|
||||||
|
E::ts('Use Matomo to track user interaction with Twingle forms'),
|
||||||
|
FALSE
|
||||||
|
);
|
||||||
|
|
||||||
$this->addButtons([
|
$this->addButtons([
|
||||||
[
|
[
|
||||||
'type' => 'submit',
|
'type' => 'submit',
|
||||||
|
|
75
CRM/TwingleCampaign/Utils/MatomoSnippet.php
Normal file
75
CRM/TwingleCampaign/Utils/MatomoSnippet.php
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
<?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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use CRM_TwingleCampaign_ExtensionUtil as E;
|
use CRM_TwingleCampaign_ExtensionUtil as E;
|
||||||
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
|
use CRM_TwingleCampaign_Utils_ExtensionCache as Cache;
|
||||||
|
use CRM_TwingleCampaign_Utils_MatomoSnippet as MatomoSnippet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TwingleForm.Get API specification (optional)
|
* TwingleForm.Get API specification (optional)
|
||||||
|
@ -110,16 +111,49 @@ function civicrm_api3_twingle_form_Get(array $params): array {
|
||||||
'project_type' => $value[$custom_field_mapping['twingle_project_type']],
|
'project_type' => $value[$custom_field_mapping['twingle_project_type']],
|
||||||
'counter' => $value[$custom_field_mapping['twingle_project_counter']]
|
'counter' => $value[$custom_field_mapping['twingle_project_counter']]
|
||||||
];
|
];
|
||||||
|
$matomo_integration_enabled = Civi::settings()->get('twinglecampaign_matomo_integration', False);
|
||||||
switch ($value[$custom_field_mapping['twingle_project_type']]) {
|
switch ($value[$custom_field_mapping['twingle_project_type']]) {
|
||||||
case 'event':
|
case 'event':
|
||||||
$returnValues[$value['id']]['embed_code'] =
|
if ($matomo_integration_enabled) {
|
||||||
$value[$custom_field_mapping['twingle_project_eventall']];
|
$returnValues[$value['id']]['embed_code'] =
|
||||||
|
MatomoSnippet::append_code(
|
||||||
|
$value[$custom_field_mapping['twingle_project_eventall']],
|
||||||
|
MatomoSnippet::get_event_tracker()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$returnValues[$value['id']]['embed_code'] =
|
||||||
|
$value[$custom_field_mapping['twingle_project_eventall']];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'shop':
|
||||||
|
if ($matomo_integration_enabled) {
|
||||||
|
$returnValues[$value['id']]['embed_code'] =
|
||||||
|
MatomoSnippet::append_code(
|
||||||
|
$value[$custom_field_mapping['twingle_project_widget']],
|
||||||
|
MatomoSnippet::get_ecommerce_tracker()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$returnValues[$value['id']]['embed_code'] =
|
||||||
|
$value[$custom_field_mapping['twingle_project_widget']];
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$returnValues[$value['id']]['embed_code'] =
|
if ($matomo_integration_enabled) {
|
||||||
$value[$custom_field_mapping['twingle_project_widget']];
|
$returnValues[$value['id']]['embed_code'] =
|
||||||
|
MatomoSnippet::append_code(
|
||||||
|
$value[$custom_field_mapping['twingle_project_widget']],
|
||||||
|
MatomoSnippet::get_event_tracker()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$returnValues[$value['id']]['embed_code'] =
|
||||||
|
$value[$custom_field_mapping['twingle_project_widget']];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return civicrm_api3_create_success($returnValues, $query, 'TwingleForm', 'Get');
|
return civicrm_api3_create_success($returnValues, $query, 'TwingleForm', 'Get');
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -26,6 +26,11 @@
|
||||||
<div class="content">{$form.twinglecampaign_soft_credits.html}</div>
|
<div class="content">{$form.twinglecampaign_soft_credits.html}</div>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="crm-section">
|
||||||
|
<div class="label">{$form.twinglecampaign_matomo_integration.label}</div>
|
||||||
|
<div class="content">{$form.twinglecampaign_matomo_integration.html}</div>
|
||||||
|
<div class="clear"></div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue