Answers.Body: <p>I wrote this <a href="https://github.com/coopsymbiotic/actiontracker" rel="nofollow noreferrer">actiontracker</a> extension a few months ago, but I have to admit that I do not use it often, so your mileage may vary.</p>
  
  <p>It generates tracking URLs for every link in the email that has a checksum. It has only been tested with flexmailer.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T21:25:05.837Z
Answers.LastActivityDate: 2019-03-07T21:25:05.837Z
Answers.UserDisplayName: bgm
Answers.Body: <p>We've accomplished this by modifying the core function <code>getTokenData()</code> to track non-tokenised part of the URL string. For eg. with the url posted in the question -</p>
  
  <p><code>https://site-name/info?cid1=&lt;contact_id_token&gt;&amp;cs=&lt;contact_checksum&gt;</code></p>
  
  <ul>
  <li>Grab the main URL before the <code>?</code> (<code>https://site-name/info</code>) and get the trackable url of this string from <code>CRM_Mailing_BAO_TrackableURL::getTrackerURL()</code> function.</li>
  <li>This enters the URL in <code>civicrm_mailing_trackable_url</code> table.</li>
  <li>Replace the HTML with this trackable URL so that main URL that is sent is in the form -
  <code>http://site-name/sites/all/modules/civicrm/extern/url.php?u=13&amp;qid=17&amp;cid1=&lt;contact_id_token&gt;&amp;cs=&lt;contact_checksum&gt;</code>, i.e, trackable url + query string from the original URL string.</li>
  <li>When the user clicks the link in the mail, user gets navigated to trackable URL and is redirected to the original URL from <code>civicrm_mailing_trackable_url</code> table. </li>
  <li>The extra query string automatically gets appended to the main URL.</li>
  </ul>
  
  <p>The above is working correctly for us and tracks each click when the URL <code>https://site-name/info</code> is loaded.</p>
  
  <p>Overriden the file in an extension. Sample Code diff looks like -</p>
  
  <pre><code>$ git diff CRM/Mailing/BAO/Mailing.php
  diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php
  index 3a0f08048c..f2501a0b91 100644
  --- a/CRM/Mailing/BAO/Mailing.php
  +++ b/CRM/Mailing/BAO/Mailing.php
  @@ -1348,6 +1348,21 @@ ORDER BY   civicrm_email.is_bulkmail DESC
         elseif (preg_match('/^href[ ]*=[ ]*\"/', $url)) {
           $url .= '"';
         }
  +      if (!empty($token_a['embed_parts'][0])) {
  +        $string = ' ' . $token_a['embed_parts'][0];
  +        $start = 'http';
  +        $end = '?';
  +
  +        $ini = strpos($string, $start);
  +        if ($ini != 0) {
  +          $ini += strlen($start);
  +          $len = strpos($string, $end, $ini) - $ini;
  +
  +          $partialLink = "http" . substr($string, $ini, $len);
  +          $trackableLink = CRM_Mailing_BAO_TrackableURL::getTrackerURL($partialLink, $this-&gt;id, $event_queue_id);
  +          $url = str_replace("{$partialLink}?", "{$trackableLink}&amp;", $url);
  +        }
  +      }
         $data = $url;
         // CRM-20206 Fix ampersand encoding in plain text emails
         if (empty($html)) {
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T15:54:16.883Z
Answers.Text: This does not seem to work if you have flexmailer installed (e.g. for Mosaico) - got  a patch for that?
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T20:02:58.100Z
Answers.Text: Here's one... https://github.com/artfulrobot/org.civicrm.flexmailer/tree/issue/30
Answers.UserDisplayName: artfulrobot
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-07T06:19:40.030Z
Answers.LastActivityDate: 2018-04-07T20:27:57.340Z
Answers.LastEditDate: 2018-04-07T20:27:57.340Z
Answers.UserDisplayName: jitendra - fuzion
Body: <p>Is it possible for CiviMail to track embedded URLs? For eg, I've a URL which contains contact id and checksum tokens in it. I need to check how many users have clicked the URL from the email.</p>
  
  <p>URL to track - <code>https://site-name/info?cid1=&lt;contact_id_token&gt;&amp;cs=&lt;contact_checksum&gt;</code></p>
  
  <p>Currently, the code in CiviMail don't consider such tokenized URL from tracking and don't insert it in <code>civicrm_mailing_trackable_url</code> table. </p>
  
  <p>There is a similar question on SE - <a href="https://civicrm.stackexchange.com/questions/19565/tracking-links-that-have-been-added-via-tokens">Tracking links that have been added via tokens</a> but it seems to provide different track links for each tokenised URL which is a pain when number of recipients is large in number. Eg 10k contacts would have 10k trackable links and will considerably increase the size of the database.</p>
  
  <p>Our thought now is to cut the URL till a token is reached and consider this for tracking. For eg, if the original URL is <code>https://site-name/info?cid1=&lt;contact_id_token&gt;&amp;cs=&lt;contact_checksum&gt;</code>, we need to modify the functionality to track only <code>https://site-name/info</code> and if such url is hit by the user, the click counter should be incremented.</p>
  
  <p>To proceed with the above, we can store the partial link in <code>civicrm_mailing_trackable_url</code> table and replace the main html URL with the trackable link. Now, when the user hits this url in the mail, the main problem arises as we don't have any data of what should be appended to the partial URL to navigate user the original page.</p>
  
  <p>Is there any another solution possible?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-17T16:48:11.247Z
Comments.Text: Has there been any progress on this since this was posted almost 3 years ago?
Comments.UserDisplayName: themak
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-02-19T03:43:34.313Z
Comments.Text: There's a patch below for normal mailing created via civimail. If you use flexmailer, i think artfulrobot's comment should help? https://civicrm.stackexchange.com/questions/24407/track-url-containing-tokens-in-civimail#comment33617_24420
Comments.UserDisplayName: jitendra - fuzion
Tags: <civimail><tracked-links>
Title: Track URL containing tokens in CiviMail
ThreadText: # Track URL containing tokens in CiviMail
  
  (Asked by: username_0 on 2018-04-06)
  
  Is it possible for CiviMail to track embedded URLs? For eg, I've a URL which contains contact id and checksum tokens in it. I need to check how many users have clicked the URL from the email.
  
  URL to track - `https://site-name/info?cid1=&cs=`
  
  Currently, the code in CiviMail don't consider such tokenized URL from tracking and don't insert it in `civicrm_mailing_trackable_url` table.
  
  There is a similar question on SE - [Tracking links that have been added via tokens](https://civicrm.stackexchange.com/questions/19565/tracking-links-that-have-been-added-via-tokens) but it seems to provide different track links for each tokenised URL which is a pain when number of recipients is large in number. Eg 10k contacts would have 10k trackable links and will considerably increase the size of the database.
  
  Our thought now is to cut the URL till a token is reached and consider this for tracking. For eg, if the original URL is `https://site-name/info?cid1=&cs=`, we need to modify the functionality to track only `https://site-name/info` and if such url is hit by the user, the click counter should be incremented.
  
  To proceed with the above, we can store the partial link in `civicrm_mailing_trackable_url` table and replace the main html URL with the trackable link. Now, when the user hits this url in the mail, the main problem arises as we don't have any data of what should be appended to the partial URL to navigate user the original page.
  
  Is there any another solution possible?
  
  **Tags:** civimail, tracked-links
  
  > **Comments:**
  > **username_1**: Has there been any progress on this since this was posted almost 3 years ago?  
  > **username_0**: There's a patch below for normal mailing created via civimail. If you use flexmailer, i think username_3's comment should help? https://civicrm.stackexchange.com/questions/24407/track-url-containing-tokens-in-civimail#comment33617_24420  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2018-04-07. Score: 6 | Accepted answer
  
  We've accomplished this by modifying the core function getTokenData() to track non-tokenised part of the URL string. For eg. with the url posted in the question -
  https://site-name/info?cid1=&cs=
  
  Grab the main URL before the ? (https://site-name/info) and get the trackable url of this string from CRM_Mailing_BAO_TrackableURL::getTrackerURL() function.
  This enters the URL in civicrm_mailing_trackable_url table.
  Replace the HTML with this trackable URL so that main URL that is sent is in the form -
  http://site-name/sites/all/modules/civicrm/extern/url.php?u=13&qid=17&cid1=&cs=, i.e, trackable url + query string from the original URL string.
  When the user clicks the link in the mail, user gets navigated to trackable URL and is redirected to the original URL from civicrm_mailing_trackable_url table. 
  The extra query string automatically gets appended to the main URL.
  
  The above is working correctly for us and tracks each click when the URL https://site-name/info is loaded.
  Overriden the file in an extension. Sample Code diff looks like -
  $ git diff CRM/Mailing/BAO/Mailing.php
  diff --git a/CRM/Mailing/BAO/Mailing.php b/CRM/Mailing/BAO/Mailing.php
  index 3a0f08048c..f2501a0b91 100644
  --- a/CRM/Mailing/BAO/Mailing.php
  +++ b/CRM/Mailing/BAO/Mailing.php
  @@ -1348,6 +1348,21 @@ ORDER BY   civicrm_email.is_bulkmail DESC
         elseif (preg_match('/^href[ ]*=[ ]*\"/', $url)) {
           $url .= '"';
         }
  +      if (!empty($token_a['embed_parts'][0])) {
  +        $string = ' ' . $token_a['embed_parts'][0];
  +        $start = 'http';
  +        $end = '?';
  +
  +        $ini = strpos($string, $start);
  +        if ($ini != 0) {
  +          $ini += strlen($start);
  +          $len = strpos($string, $end, $ini) - $ini;
  +
  +          $partialLink = "http" . substr($string, $ini, $len);
  +          $trackableLink = CRM_Mailing_BAO_TrackableURL::getTrackerURL($partialLink, $this->id, $event_queue_id);
  +          $url = str_replace("{$partialLink}?", "{$trackableLink}&", $url);
  +        }
  +      }
         $data = $url;
         // CRM-20206 Fix ampersand encoding in plain text emails
         if (empty($html)) {
  
  > **Comments:**
  > **username_3**: This does not seem to work if you have flexmailer installed (e.g. for Mosaico) - got  a patch for that?  
  > **username_3**: Here's one... https://github.com/username_3/org.civicrm.flexmailer/tree/issue/30  
  
  ---
  
  ### Answer by username_2 on 2019-03-07. Score: 0
  
  I wrote this [actiontracker](https://github.com/coopsymbiotic/actiontracker) extension a few months ago, but I have to admit that I do not use it often, so your mileage may vary.
  
  It generates tracking URLs for every link in the email that has a checksum. It has only been tested with flexmailer.
  
  
  ---
  
  