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 3rd party analytics platforms like Google Analytics. Learn more about system variables.

You also have the option to set the link append as a variable and have it cascade through all the links in the message.

Observe the following examples and remember you can see them work in real time with the message preview feature.

The 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>

Note the question mark is automatically added by the modifier.

A 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

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

A 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

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

Removing 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

Note that 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

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

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

Using 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

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

Creating UTM Parameters Efficiently

Here is 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 learn about updating link membership when clicking a link.

Comments

0 comments

Please sign in to leave a comment.