This is kind of the opposite of 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?

  • 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.
    – Alan Dixon
    Commented Aug 25, 2016 at 16:35
  • 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.
    – Alan Dixon
    Commented Aug 25, 2016 at 16:37

1 Answer 1


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.

  • Brilliant, thanks, just what I was looking for, works.
    – Alan Dixon
    Commented Sep 7, 2016 at 18:43
  • Glad it helped.
    – Andrei
    Commented Sep 7, 2016 at 18:44
  • 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!
    – Alan Dixon
    Commented Sep 7, 2016 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.