How can we help?

Link appends via Smarty

Overview

Cordial provides a way to add link appends to your URLs with the Smarty modifier appendQueryString. You can include Cordial system variables such as message name within the query string for tracking in third-party analytics platforms like Google Analytics.

You also have the option to set the link append as a variable and have it cascade through all the links in the message. Check out the examples below for specific guidance.

Entire query string is appended

Smarty:

<a href="{'https://example.com'|appendQueryString:'utm_campaign=example123'}">Click Me</a>

Rendered output:

<a href="https://example.com?utm_campaign=example123">Click Me</a>

The question mark is automatically added by the modifier.

Portion of the query is appended

{'https://example.com?utm_source=example123'|appendQueryString:'utm_campaign=example345'} 

Rendered output:

https://example.com?utm_source=example123&utm_campaign=example345

The modifier detects that the question mark exists and automatically uses the ampersand delimiter.

Portion of the query string is appended as a variable

{'https://example.com?utm_source=example123'|appendQueryString:'utm_campaign='|cat:$message.name} 

Rendered output:

https://example.com?utm_source=example123&utm_campaign=newsletter

The variable is replaced by the name you gave the message (newsletter) in the system.

Remove spaces from variables

If the value for the variable has spaces in it (i.e. message name = "weekly specials"), you can use a modifier to replace those spaces with an underscore.

Smarty:

{'https://example.com?utm_source=example123'|appendQueryString:'utm_campaign='|cat:$message.name|replace:' ':'_'}

Rendered output:

https://example.com?utm_source=example123&utm_campaign=weekly_specials

The message name value "weekly specials" was changed to "weekly_specials"

Set the link append as a variable

You can also set the link append as a variable within the message once, and then populate all of the links within the message automatically. This makes it possible to edit the link append one time and have it cascade through all of the links.

Set the variable and value with Smarty at the top of your message content:

{$linkAppend = 'utm_source=cordial&utm_medium=email&utm_campaign='|cat:$message.name|replace:' ':'_'}

Then add the variable to each link in the HTML of the message:

<a href="{'http://example.com'|appendQueryString:$linkAppend}">Example</a>
<a href="{'http://example.com?id=123'|appendQueryString:$linkAppend}" target="blank">Example</a>

Rendered output:

https://example.com?utm_source=cordial&utm_medium=email&utm_campaign=weekly_specials
https://example.com?id=123&utm_source=cordial&utm_medium=email&utm_campaign=weekly_specials

The variable is replaced in each link with the value that was set and that the correct delimiter is used (question mark or ampersand).

When using appendQueryString for links, it's necessary to name the tracked links using Smarty, otherwise they will appear as a single curly brace "{" on the link performance page.

Use variables in URLs with link append

If you would like to set a variable within a URL and use link append, you will need to define the data prior to the href line of code. This is because the single quotes around the URL inside of the href prevent the use of variables. 

Set the variable and build the URL with Smarty at the top of your message content:

{$data.code = "ABC123"}
{$data.path = "https://example.com/{$data.code}/other/parts/of/path"}
{$linkAppend = 'utm_source=cordial&utm_medium=email&utm_campaign='|cat:$message.name|replace:' ':'_'}

Then add the URL variable to the link in the HTML of the message:

<a href="{$data.path|appendQueryString:$linkAppend}">Example</a>

Rendered output:

https://example.com/ABC123/other/parts/of/path?utm_source=cordial&utm_medium=email&utm_campaign=weekly_specials

The first path name is replaced in the URL because the data was built up prior to the href line of code.

Create UTM parameters efficiently

Here's an example code for adding UTM parameters efficiently. Note that we are using the Smarty {strip} tag to avoid printing code formatting spaces within a message.

{strip}
    {$utm.utm_campaign = 'Trigger-SA_1'}
    {$utm.cvosrc = 'email.trigger.SearchAbandon'}
    {$utm.utm_medium = 'Email'}
    {$utm.srccode = 'EM_TR_SA'}
    {$utm.utm_source = 'Trigger'}
    {$utm.obem = 'D-KR4ZJw7A_IpHEC33hf1w=='}
    {foreach $utm as $key => $value}
        {if $value@iteration > 1}
            {$data.utm_params = $data.utm_params|cat:'&'}
        {/if}
        {$data.utm_params = $data.utm_params|cat:$key|cat:'='|cat:urlencode($value)}
    {/foreach}
{/strip}

Then add the URL variable to the link in the HTML of the message:

<a href="{'http://www.example.com'|appendQueryString:$data.utm_params}">Example</a>

Each new UTM parameter added using $utm_yourUTM will be included in the utm_params string. The string generated by the above example would look like this:

utm_campaign=Trigger-SA_1&cvosrc=email.trigger.SearchAbandon&utm_medium=Email&srccode=EM_TR_SA&utm_source=Trigger&obem=D-KR4ZJw7A_IpHEC33hf1w%3D%3D

In the next article, you can learn about updating link membership when clicking a link.

Comments

0 comments

Please sign in to leave a comment.