-
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.
[#2] implemented anonymous click tracking (WIP)
- Loading branch information
Showing
14 changed files
with
413 additions
and
50 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
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,97 @@ | ||
<?php | ||
/*-------------------------------------------------------+ | ||
| SYSTOPIA Mailingtools Extension | | ||
| Copyright (C) 2019 SYSTOPIA | | ||
| Author: B. Endres ([email protected]) | | ||
+--------------------------------------------------------+ | ||
| This program is released as free software under the | | ||
| Affero GPL license. You can redistribute it and/or | | ||
| modify it under the terms of this license which you | | ||
| can read by viewing the included agpl.txt or online | | ||
| at www.gnu.org/licenses/agpl.html. Removal of this | | ||
| copyright header is strictly prohibited without | | ||
| written permission from the original author(s). | | ||
+--------------------------------------------------------*/ | ||
|
||
|
||
use CRM_Mailingtools_ExtensionUtil as E; | ||
|
||
/** | ||
* Processor for anonymous url tracking | ||
*/ | ||
class CRM_Mailingtools_AnonymousURL { | ||
|
||
/** | ||
* Process an anonymous url tracking event | ||
* | ||
* @param $trackable_url_id int URL id | ||
* @return string|null the URL this ID belongs to | ||
* @throws Exception if something failed. | ||
*/ | ||
public static function processAnonymousClickEvent($trackable_url_id) { | ||
$config = CRM_Mailingtools_Config::singleton(); | ||
|
||
// check if we're enabled | ||
$enabled = $config->getSetting('anonymous_link_enabled'); | ||
if (!$enabled) { | ||
return NULL; | ||
} | ||
|
||
// check the link ID... | ||
$trackable_url_id = (int) $trackable_url_id; | ||
if (!$trackable_url_id) { | ||
throw new Exception("Bad link ID"); | ||
} | ||
// load the link | ||
$link = CRM_Core_DAO::executeQuery(" | ||
SELECT mailing_id, url | ||
FROM civicrm_mailing_trackable_url | ||
WHERE id = %1", [1 => [$trackable_url_id, 'Integer']]); | ||
if (!$link->fetch()) { | ||
throw new Exception("Invalid link ID"); | ||
} | ||
|
||
// NOW: find a matching event queue ID | ||
$event_queue_id = CRM_Mailingtools_AnonymousOpen::getEventQueueID($link->mailing_id, 'anonymous_link_contact_id'); | ||
if (empty($event_queue_id)) { | ||
throw new Exception("No found event in queue for mailing [{$link->mailing_id}]"); | ||
} | ||
|
||
// all good: add entry | ||
CRM_Core_Error::debug_log_message("Tracked anonymous click event for link {$trackable_url_id} in mailing [{$link->mailing_id}]"); | ||
CRM_Core_DAO::executeQuery(" | ||
INSERT INTO civicrm_mailing_event_trackable_url_open (event_queue_id, trackable_url_id, time_stamp) | ||
VALUES (%1, %2, NOW())", [ | ||
1 => [$event_queue_id, 'Integer'], | ||
2 => [$trackable_url_id, 'Integer']]); | ||
|
||
return $link->url; | ||
} | ||
|
||
|
||
|
||
/** | ||
* This function will manipulate open tracker URLs in emails, so they point | ||
* to the anonymous handler instead of the native one | ||
*/ | ||
public static function modifyEmailBody(&$body) { | ||
$config = CRM_Mailingtools_Config::singleton(); | ||
if (!$config->getSetting('anonymous_link_enabled') | ||
|| !$config->getSetting('anonymous_link_url')) { | ||
// NOT ENABLED | ||
return; | ||
} | ||
|
||
// get the base URL | ||
$core_config = CRM_Core_Config::singleton(); | ||
$system_base = $core_config->userFrameworkBaseURL; | ||
|
||
// find all all relevant links and collect queue IDs | ||
if (preg_match_all("#{$system_base}sites/all/modules/civicrm/extern/url.php\?u=(?P<link_id>[0-9]+)[^'\"\\n]+#i", $body, $matches)) { | ||
foreach ($matches[0] as $i => $string) { | ||
$new_url = $config->getSetting('anonymous_link_url') . "?u={$matches['link_id'][$i]}"; | ||
$body = str_replace($string, $new_url, $body); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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,54 @@ | ||
<?php | ||
/*-------------------------------------------------------+ | ||
| SYSTOPIA Mailingtools Extension | | ||
| Copyright (C) 2018-2019 SYSTOPIA | | ||
| Author: B. Endres ([email protected]) | | ||
+--------------------------------------------------------+ | ||
| This program is released as free software under the | | ||
| Affero GPL license. You can redistribute it and/or | | ||
| modify it under the terms of this license which you | | ||
| can read by viewing the included agpl.txt or online | | ||
| at www.gnu.org/licenses/agpl.html. Removal of this | | ||
| copyright header is strictly prohibited without | | ||
| written permission from the original author(s). | | ||
+--------------------------------------------------------*/ | ||
|
||
use CRM_Mailingtools_ExtensionUtil as E; | ||
|
||
|
||
/** | ||
* API: Mailingtools.Anonurl | ||
* | ||
* Processes an anonymous click event on a trackable URL | ||
* based only on the mailing ID and the link ID | ||
* | ||
* @param array $params containing 'mid' | ||
* @return array result; | ||
* @throws CiviCRM_API3_Exception | ||
*/ | ||
function civicrm_api3_mailingtools_anonurl($params) { | ||
try { | ||
$url = CRM_Mailingtools_AnonymousURL::processAnonymousClickEvent($params['u']); | ||
if ($url) { | ||
$link_id = (int) $params['u']; | ||
return civicrm_api3_create_success([$link_id => $url]); | ||
} else { | ||
return civicrm_api3_create_success("Anonymous click tracking disabled."); | ||
} | ||
} catch (Exception $ex) { | ||
throw new CiviCRM_API3_Exception($ex->getMessage(), $ex->getCode()); | ||
} | ||
} | ||
|
||
/** | ||
* API Specs: Mailingtools.Anonopen | ||
*/ | ||
function _civicrm_api3_mailingtools_anonurl_spec(&$spec) { | ||
$spec['u'] = array( | ||
'name' => 'u', | ||
'api.required' => 1, | ||
'type' => CRM_Utils_Type::T_INT, | ||
'title' => 'URL ID', | ||
'description' => 'URL ID for which the click event should be recorded', | ||
); | ||
} |
Oops, something went wrong.