Did you have setup the Parameter Management in Marketing Cloud? If yes, the Marketing Cloud subscriberKey is added to the click out URL’s. Next step is to catch the parameter when a SFMC subscriber clicks on a link in an email and navigates to your website.
In our example we will be using:
mcparam=%%subscriberkey%%
Example Google Tag Manager setup
Creating custom variable for query parameter
To store the parameter as a variable take the following steps:
- Navigate to your GTM container
- Navigate to ‘Variables’
- Add a new ‘User defined variable’
- Configure the variable type as a ‘URL’
- Choose component type ‘Query’
- In the ‘Query Key’ field enter the parameter you used in the Parameter Management
- Save the variable
Configure trigger subscriberKey Marketing Cloud
To listen for the parameter we need to setup a trigger. This trigger will fire when the parameter is found.
- Go to ‘Triggers’
- Add new trigger
- Choose Trigger Type ‘Page View’
- Fire trigger on ‘Some Page Views’
- Include the newly created variable ‘SubscriberParameter’
- Choose dos not match Regex ‘^(undefined|null|0|false|NaN|)$’
- Save trigger
{{Variable}} does not match RegEx (ignore case) ^(undefined|null|0|false|NaN|)$
Store subscriberKey in cookie via new tag
To be able to also use the subscriberKey after the first pageview, you can store the subscriberKey in a cookie. To do so follow these steps.
- Navigate to ‘Tags’
- Create new tag
- Choose ‘Custom HTML’
- Use your own script or use our example cookie script below
- Use the trigger we created above ‘mcparam query’
Example cookie javaScript
<script>
var cookieName = "mcparam";
var cookieValue = {{subscriberParameter}};
var cookiePath = "/";
var domain = ".youdomain.com";
var expirationTime = 31536000; //For example one month in seconds (2628000)
expirationTime = expirationTime * 1000; //Convert expirationtime to milliseconds
var date = new Date(); //Create javascript date object
var dateTimeNow = date.getTime(); //Get current time in milliseconds since 1 january 1970 (Unix time)
date.setTime(dateTimeNow + expirationTime); //Set expiration time (Time now + one month)
var expirationTime = date.toUTCString(); //Convert milliseconds to UTC time string
document.cookie = cookieName+"="+cookieValue+"; expires="+expirationTime+"; domain="+domain+"; path="+cookiePath; //Set cookie
</script>
Add custom variable to capture cookie value
Last step is to store the cookie value in a custom variable for future use. Follow the steps below to do so.
- Navigate to ‘Variables’
- Add a new ‘User defined variable’
- Configure the variable type as a ‘1st Party Cookie’
- Enter ‘mcparam’ as the Cookie Name
- Save the variable
Next step using the variable in Google Tag Manager tags
After creating an interaction in Activation Studio and exporting the TPL file. You can configure the tag to include the subscriberKey. This is mandatory to link interactions in Marketing Cloud with subscribers.
- Setup new Tag
- Configure the variable fields
- Link SubscriberKey with the custom variable ‘mcparam’ created above
- Choose a trigger; for example all pageviews
- Use an exception for when the variable ‘mcparam’ is null
Creating an exception trigger
To make sure we won’t trigger when the variable is empty, you can setup an exception trigger.
{{Variable}} matches RegEx (ignore case) ^(undefined|null|0|false|NaN|)$