Zig
Member
My team is currently working on an addon that adds analytics markers to the links in email templates. Our current solution is to use a callback in the template and pass in the URL constructed by
Front end code:
Back end code:
The
Have I missed something here? Is there perhaps an easier way to achieve the desired result?
{{ link('foo:bar', $baz) }}
as a parameter. Unfortunately, this is producing some rather unexpected results. Here's an example of the code involved...Front end code:
Code:
<xf:set var="$url">{{ link('canonical:index') }}</xf:set>
<xf:set var="$analyticsUrl"><xf:callback class="Our\Callback\Class" method="getAnalyticsLink" params="[$url, $template.title]"></xf:callback>"></xf:set>
{{ dump($url) }}
{{ dump($analyticsUrl) }}
Back end code:
Code:
public static function getAnalyticsLink($params)
{
$url = $params[0];
$templateTitle = $params[1];
$parts = parse_url($url);
if ($campaignName = \XF::options()->googleAnalyticsCampaignName) {
$url = sprintf('%s://%s%s?utm_campaign=%s&utm_medium=email&utm_source=%s%s%s',
$parts['scheme'], $parts['host'], $parts['path'],
urlencode($campaignName), urlencode($templateTitle),
(empty($parts['query']) ? '' : '&'.$parts['query']),
(empty($parts['fragment']) ? '' : '#'.$parts['fragment'])
);
}
return htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
}
The
{{ dump($url) }}
outputs exactly what you would expect: https://our.domain.name/
. The {{ dump($analyticsUrl) }}
however outputs ://?utm_campaign=TestCampaignName&utm_medium=email&utm_source=">
. Since neither the original URL or the template title are making it into the callback output, it seems the parameters aren't making it to the back end at all.Have I missed something here? Is there perhaps an easier way to achieve the desired result?