GTM

How to Store an Affiliate ID in a Cookie via Google Tag Manager

Need to store an affiliate ID sending traffic to your site and use this data in reports to measure their performance?

How to Store an Affiliate ID in a Cookie via Google Tag Manager

Brad Redding

Brad Redding is the Founder & CEO of Elevar. Specializing in analytics, tracking, GTM, and conversion optimization.

Many of our customers have affiliate programs that rely on hundreds (or thousands) of affiliate partners to drive traffic and conversions.

But these partner specific IDs are not always available in Google Analytics hits and can’t be used in various tags if you wanted to monitor/track by affiliate partner. All you see in GA is a source medium like “cj / affiliate” and potentially campaign ID but you usually won’t get the partner ID because it is in a custom query parameter.

These might need to be used to pass back the partner ID to the affiliate, track what partners are your best performers, who’s skimming the most revenue off the top, etc.

This quick guide will show how you can store affiliate IDs in a cookie to use in any tag including a custom transaction event in Google Analytics.

5 Steps to store an affiliate ID to be used in Google Analytics:

  1. Create a query parameter variable that contains your unique affiliate ID’s
  2. Create a custom HTML tag that sets a 1st party cookie with your affiliate data
  3. Create a custom trigger that fires this cookie when visits come from affiliates
  4. Create your new cookie as a variable
  5. Create your custom event that includes this variable in your event data

Step 1: Create Query Parameter Variable with Affiliate ID

Inside of GTM > Variables > Custom Defined Variable create a new URL variable with Query set as the component type.

Then you’ll need to look for the query parameter that is in the URL people click on from your affiliates to visit your site.

For example it may look like this: https://www.getelevar.com?utm_source=cj&utm_medium=affiliate&affiliateId=4353212

Where affiliateId is the unique affiliate ID you are looking to track.

This is what this variable should look like:

unique-affiliate-id

Step 2: Create Custom HTML Tag that sets 1st Party Cookie

Next up you need to create a custom HTML tag inside of GTM that stores this variable in a cookie.

There are two things you need to modify in this tag below:

  1. The cookieName: this can be anything really, I just named it affiliateId to keep it simple
  2. The cookieValue: this is the variable that you just created in step 1.

Here’s the code snippet:

<script>
var cookieName = "affiliateId"; // Name of your affiliate cookie
var cookieValue = "{{Query URL - Affiliate ID}}"; // Sets your unique affiliate visit ID
var expirationTime = 2592000; // Expiration date - one month in seconds
expirationTime = expirationTime * 1000; // Converts expirationtime to milliseconds
var date = new Date(); 
var dateTimeNow = date.getTime();

date.setTime(dateTimeNow + expirationTime); // Sets expiration time (Time now + one month)
var date = date.toUTCString(); // Converts milliseconds to UTC time string
document.cookie = cookieName+"="+cookieValue+"; expires="+date+"; path=/; domain=." + location.hostname.replace(/^www\./i, ""); // Sets cookie for all subdomains
</script>

Here is what the tag looks like:

affiliate-cookie-id-tag

Step 3: Create Trigger that Fires Cookie on Affiliate Visit

In this step you’re going to create a custom trigger that fires the tag from step 2 only when we detect a visit where our affiliateId query parameter exists in the URL.

Pretty simple – just create a new page view trigger with the fire logic of Page URL matches RegEx of your query parameter that is in the URL.

affiliate-query-param-trigger

Step 4: Create Cookie Variable

The last step to get this ready for GA is to create our 1st party cookie that stores this value that we can then use in our various tags.

Go to variables once again and create a new 1st party cookie variable where the cookie name equals the cookie name that you set in step 1 above.

affiliate-cookie-variable

Step 5: Create Event to Send Data to Google Analytics

Now you’re ready to send this data to GA or any other data source you need.

Here’s an example of sending a transaction custom event with affiliate ID in the event action:

send affiliate id to gtm

If you have to send this data through a different custom tag then you can simply use this variable just like you would for any other variable inside of GTM.

That’s it! If you have any questions please leave a comment below.

  1. Hey,

    Thanks for the fantastic article! (:

    I do not understand completely what I need to do with cookieValue, are you able to help? How do I define this?

    Thank you kindly!
    Pablo

  2. Неllо all, guyѕ! I know, mу mеssage mау be tоo spеcіfic,
    Βut my siѕtеr found nіcе man hеre and theу mаrriеd,
    so hоw abоut mе?ǃ 🙂
    I аm 22 уeаrs old, Мargаrita, from Ukrаine, I
    know English and Germаn languаges аlso
    And… Ι havе sресifіс diseаsе, named nуmрhomаnia.
    Whо know what іѕ thiѕ, cаn undеrstand me (bеttеr tо ѕaу
    іt іmmediatеly)
    Αh yеs, I сook vеrу tаѕty! and I love not only сооk
    ;))
    Im real gіrl, nоt рrоѕtitute, аnd looking for sеrіоus аnd hot rеlationѕhір…

    Αnywaу, уou сan fіnd my prоfіle hеrе: Маrgаrіtа

Leave a Reply

Your email address will not be published. Required fields are marked *

You may also like