Answers.Body: <p>The BAO responsible for trackable URLs is <code>CRM_Mailing_BAO_TrackableURL</code> link <a href="https://github.com/civicrm/civicrm-core/blob/6c6872a8ee5acec3dba231fe2231c610241302eb/CRM/Mailing/BAO/TrackableURL.php#L56" rel="nofollow">here</a>.</p>
  
  <p>Because the <code>MailingJob</code> is available in <code>hook_civicrm_tokenValues</code> you could do something like this:</p>
  
  <pre><code>// job details to get mailing_id 
  $job_id = civicrm_api3('MailingJob', 'getsingle', array(
          'id' =&gt; $job,
  ));
  // get event_queue_id
  $queue_id = civicrm_api3('MailingEventQueue', 'getsingle', array(
          'job_id' =&gt; $job,
          'contact_id' =&gt; $cid,
  ));
  
  $url = 'https://example.com';
  $trackableUrl = CRM_Mailing_BAO_TrackableURL::getTrackerURL($url, $job_id['mailing_id'], $queue_id['id']);
  </code></pre>
  
  <p>RE: "wyswyig button that pulled in auto-generated content" 
  <a href="http://ckeditor.com/addon/htmlbuttons" rel="nofollow">HTML Buttons</a> plugin for CKEditor does what you describe, I have a couple of dropdowns with buttons that insert complex HTML snippets, is what I use for composing newsletters.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-07T18:43:38.963Z
Answers.Text: Brilliant, thanks, just what I was looking for, works.
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-07T18:44:44.657Z
Answers.Text: Glad it helped.
Answers.UserDisplayName: Andrei
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-07T21:08:43.400Z
Answers.Text: Whoops - this worked on a test, but not for a real mailing. The missing bit was to get the queue_id with the extra parameter of contact_id => cid. In other words, the event queue id is not unique per job id, it has be to pulled separately for each contact. But then it worked!
Answers.UserDisplayName: Alan Dixon
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-07T15:59:15.183Z
Answers.LastActivityDate: 2016-09-19T09:29:58.243Z
Answers.LastEditDate: 2016-09-19T09:29:58.243Z
Answers.UserDisplayName: Andrei
Body: <p>This is kind of the opposite of <a href="https://civicrm.stackexchange.com/questions/8226/integrating-civimail-tracked-links-with-google-analytics">Integrating CiviMail tracked links with Google Analytics</a></p>
  
  <p>In other words, I'm generating html for civimail using tokens, and the html includes links, but those links are not getting converted into trackable links. I think there's probably some code I could include in my hook_civicrm_tokenValues function to convert my embedded urls into trackable links, just like the Civi core code does. Or maybe I have to tell Civi that it's okay to do this using some settings?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-25T16:35:45.697Z
Comments.Text: On a related question - is it possible to create custom 'domain' tokens, i.e. ones that are only calculated once for the mailing? Judging from a scan in the code for Hook::tokenValues, the answer seems to be no.
Comments.UserDisplayName: Alan Dixon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-08-25T16:37:14.170Z
Comments.Text: And further afield - maybe this is the wrong strategy for including auto-generated content? An alternative would be a wyswyig button that pulled in auto-generated content, giving the author full control.
Comments.UserDisplayName: Alan Dixon
Tags: <civimail><tokens>
Title: How do I create a token that automatically gets urls convert to trackable links?
ThreadText: # How do I create a token that automatically gets urls convert to trackable links?
  
  (Asked by: username_0 on 2016-08-25)
  
  This is kind of the opposite of [Integrating CiviMail tracked links with Google Analytics](https://civicrm.stackexchange.com/questions/8226/integrating-civimail-tracked-links-with-google-analytics)
  
  In other words, I'm generating html for civimail using tokens, and the html includes links, but those links are not getting converted into trackable links. I think there's probably some code I could include in my hook\_civicrm\_tokenValues function to convert my embedded urls into trackable links, just like the Civi core code does. Or maybe I have to tell Civi that it's okay to do this using some settings?
  
  **Tags:** civimail, tokens
  
  > **Comments:**
  > **username_0**: On a related question - is it possible to create custom 'domain' tokens, i.e. ones that are only calculated once for the mailing? Judging from a scan in the code for Hook::tokenValues, the answer seems to be no.  
  > **username_0**: And further afield - maybe this is the wrong strategy for including auto-generated content? An alternative would be a wyswyig button that pulled in auto-generated content, giving the author full control.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-09-07. Score: 3 | Accepted answer
  
  The BAO responsible for trackable URLs is CRM_Mailing_BAO_TrackableURL link here.
  Because the MailingJob is available in hook_civicrm_tokenValues you could do something like this:
  // job details to get mailing_id 
  $job_id = civicrm_api3('MailingJob', 'getsingle', array(
          'id' => $job,
  ));
  // get event_queue_id
  $queue_id = civicrm_api3('MailingEventQueue', 'getsingle', array(
          'job_id' => $job,
          'contact_id' => $cid,
  ));
  
  $url = 'https://example.com';
  $trackableUrl = CRM_Mailing_BAO_TrackableURL::getTrackerURL($url, $job_id['mailing_id'], $queue_id['id']);
  
  RE: "wyswyig button that pulled in auto-generated content" 
  HTML Buttons plugin for CKEditor does what you describe, I have a couple of dropdowns with buttons that insert complex HTML snippets, is what I use for composing newsletters.
  
  > **Comments:**
  > **username_0**: Brilliant, thanks, just what I was looking for, works.  
  > **username_1**: Glad it helped.  
  > **username_0**: Whoops - this worked on a test, but not for a real mailing. The missing bit was to get the queue_id with the extra parameter of contact_id => cid. In other words, the event queue id is not unique per job id, it has be to pulled separately for each contact. But then it worked!  
  
  ---
  
  