Skip to main content

Lookup tables are one of the most useful and powerful features of Google Tag Manager when it comes to customising and improving your Google Analytics tracking configuration.

Note: This article was first published on rebelytics.com in 2015 and has since then been updated several times and moved to this blog.

In this article, you will find answers to the following questions:

  • What are GTM lookup tables?
  • How can I track my traffic sources correctly with the help of GTM lookup tables?
  • How can I track clicks on social icons with GTM lookup tables?
  • How can I reduce the number of tags I need using GTM lookup tables?

If you have any additional questions or if anything remains unclear, please don’t hesitate to leave a comment at the bottom of this page. I normally reply within a couple of days.

So, let’s get started and have a look at some of the cool stuff you can do with lookup tables in Google Tag Manager.

What are lookup tables?

A lookup table in Google Tag Manager is a variable that has the value of another variable as input. Variables, according to Google’s definition, are “name-value pairs for which the value is populated during runtime”.

Google Tag Manager has a number of built-in variables, such as “Page URL”, “Page Path”, or “Page Hostname”. The output value of the variable “Page URL” is the URL of the current page, so on this page it would be:

“https://www.searchviu.com/en/lookup-tables-google-tag-manager/”

The value of the variable “Page Hostname” is the hostname of the current page, so for this page it would be “www.searchviu.com”. The variable “Page Path” is the part of the “Page URL” that comes after the “Page Hostname”, so on this page it would be “/en/lookup-tables-google-tag-manager/”.

You can check the output values of all the active variables on your own website by activating the GTM preview mode and going to “Variables”. For the current page, the values of the variables mentioned above are shown like this:

On top of the built-in variables, Tag Manager allows you to define your own set of variables. Lookup tables are a type of user-defined variables and they can be very helpful when you use them in triggers (formerly known as rules), but also when you use them in tags to populate certain fields with dynamic values. Later on in this article, we will have a look at some of the possibilities lookup tables give you.

As mentioned above, lookup tables are a type of variable that have the output value of another variable as input. To get back to our “Page URL” example, I could use the output of this variable as input for a new lookup table variable that I define myself. Remember that the output of the “Page URL” variable always is the URL of the current page. In a lookup table, you always define pairs of values: If the input equals “xyz”, I want the output to be “123”. If the input equals “abc”, I want the output to be “789”, and so on.

If, for some reason, I wanted to track, let’s say, the mood I was in when I published my blog articles, I could use a lookup table to define a mood value output for every URL input and send the values to Google Analytics in a custom dimension.

This is obviously not a very realistic example for using lookup tables, but don’t worry, we will also look at some real-world examples in a bit.

So if the output of the variable “Page URL” was “https://www.searchviu.com/en/lookup-tables-google-tag-manager/”, I could use a lookup table that defines the value “grumpy” for this page view and send it to Google Analytics.

Here’s what this lookup table would look like in GTM:

I could now use the output value of this lookup table to populate a custom dimension in a Google Analytics page view tag. But let’s just have a look at some real-life examples of lookup tables that actually make sense!

Use a lookup table to track different traffic sources

The first time I came in contact with lookup tables was after reading this brilliant article on the LunaMetrics blog. Jim Gianoglio describes how you can use URL fragments to track different traffic sources in Google Analytics using a lookup table. I love this solution and I have been using it in a slightly modified way on all of my clients’ websites and also here on searchviu.com ever since.

I will quickly walk you through setting this up in Google Tag Manager (V2). The goal of this solution is to be able to track the traffic your social posts and other traffic sources generate without having to use Google’s UTM tracking parameters (the horrible ones that look like this and don’t even fit in a line on this blog:
“?utm_source=facebook.com&utm_medium=social&utm_campaign=facebook-posts”).

First, you need to set up a user defined variable of the type “URL”. In the configuration of the variable, select the component type “Fragment”. Make sure you give the variable a clear name that you will remember and recognise, like “URL fragment”. This variable will now have the URL fragment of the current page as output. Here’s what it looks like in GTM:

URL-fragment-variable-in-GTM

A URL fragment is the part of the URL that comes after the hash (#), if there is one. So now your Tag Manager configuration is able to read the URL fragments of URLs like this:

  • https://www.searchviu.com/en/lookup-tables-google-tag-manager/#twitter
  • https://www.searchviu.com/en/lookup-tables-google-tag-manager/#facebook
  • https://www.searchviu.com/en/lookup-tables-google-tag-manager/#linkedin
  • and so on

Do you see where this is heading? You can now tag the links you share on social media or in your newsletter in a much easier and more user-friendly way than with UTM tracking parameters. Next, let’s see how to get this data into Google Analytics.

So why don’t we just send the value of the “URL fragment” variable straight to Google Analytics? Because URL fragments are used for plenty of purposes and we only want to send the values we defined for our different marketing channels to Google Analytics. This is where the lookup table comes in handy: It acts as a kind of filter. Also, wouldn’t it be nice to populate the dimensions “source, “medium” and “campaign”, just like we do with UTM parameters?

Let me walk you through this step by step. First, we need three lookup tables, one for each of the dimensions “source”, “campaign” and “medium”. The input values are the same in all of them (the URL fragments we use for tracking our traffic sources), and the output values are the values we want to show up in the three dimensions “source”, “medium” and “campaign” for each of the input values.

Here’s what the lookup table for the dimension “source” would look like if we wanted to track the traffic sources for the three example URLs above:

The lookup table for the dimension “medium” looks very similar, with the only difference that the output values are changed:

And finally, the lookup table for the medium “campaign”, you get it!

Now, if I visit the URL “https://www.searchviu.com/en/lookup-tables-google-tag-manager/#twitter”, which I would use for sharing this article on Twitter, I see the following values for my new variables in the GTM preview mode:

Next, let’s have a look at the last step that is needed in order to get this data into Google Analytics. All you need to do is to go to your Google Analytics page views tag in Google Tag Manager (or to your Google Analytics settings variable, if you’re using one), and add the output values of your new lookup tables to the fields “campaignSource”, “campaignMedium” and “campaignName” in the section “Fields to set”, as you can see in this screenshot:

The URL fragments “#twitter”, “#facebook” and “#linkedin” now have the same function as the following UTM parameter chains:

  • ?utm_source=twitter.com&utm_medium=social&utm_campaign=tweets
  • ?utm_source=facebook.com&utm_medium=social&utm_campaign=facebook-posts
  • ?utm_source=linkedin.com&utm_medium=social&utm_campaign=linkedin-posts

Pretty awesome, isn’t it? Shoutouts to Jim Gianoglio for inspiring this method with the Luna Metrics article I linked to above, and to my former colleague and wizard web analyst Sascha Schau, who helped me refine this method when we were working together.

Let’s have a look at some more cool stuff you can do with lookup tables in Google Tag Manager.

Use a lookup table to track clicks on social icons

Another very interesting way to use lookup tables is for the tracking of outbound links. For the social profiles that you link to from your website, you can create a lookup table that defines a descriptive output value for each of the URLs you link to, for example “facebook profile”, “google+ profile”, and so on.

You can now use the output of this lookup table as a condition in a link click trigger and at the same time to populate the values of the events you send to Google Analytics. Here are the steps you need to undertake:

  • Create a lookup table that gives a descriptive name to the URLs of the outbound links you want to track. Use the URLs of your social profiles as inputs and the descriptive names as outputs.
  • lookup-table-for-social-profile-URLs

  • Create a new trigger with the event type “click”.
  • Choose the type “link click”.
  • Select “Fire on some clicks”, select the name of your new lookup table, and set the condition that it match a regular expression with all the output values of your lookup table, separated by vertical bars, for example: googleplus|linkedin|twitter
  • social-profile-click-trigger-GTM

  • Create a new Universal Analytics Tag and select the track type “event”.
  • Use your new lookup table to populate the fields you send to Google Analytics. It makes sense to use the output value in the field “Label” under “Event tracking parameters”.
  • Profile-click-event-tag-settings

  • In the last step of your Google Analytics tag configuration, select “fire on click” and pick your newly created link click trigger.
  • Fire-GTM-tag-on-click-event

This is so easy that it feels like cheating! But we’re already done. Your outbound link clicks are now being tracked in Google Analytics and the data even looks nice because you used a lookup table to set descriptive names.

Use lookup tables to reduce the number of tags you need

If you tried to set up the solution described above without lookup tables, you would probably succeed, but you would need a hell of a lot of tags. So lookup tables help you reduce the number of tags in this case and they do so in many other cases.

You might be using a Google Tag Manager container for several domains. Think about using a lookup table that defines an Analytics tracking ID output for a hostname input. This way you could use one Google Analytics tag to track data in several properties.

Also, with the right combination of lookup tables, you will not need different tags of the same track type any more. One pageview tag, one event tag, one transaction tag (and so on), will be enough in many cases. This makes your Tag Manager account look a lot tidier and it leaves less room for stupid mistakes.

Once you start working with lookup tables you will realise how much time they save you and also how much fun it is to quickly set up things that would have taken an hour before.

If you have any questions or thoughts about this article, please don’t hesitate to drop me a comment!

Eoghan Henn

Author Eoghan Henn

Eoghan Henn is responsible for searchVIU's marketing. Before joining searchVIU, he worked as an SEO consultant for 6 years and built a reputation as a blogger and conference speaker.

More posts by Eoghan Henn

Join the discussion 33 Comments

  • Sander says:

    Hi Eoghan,

    Thank you for this great post! I have a question, and I think it is kind of simple, but till now I didn’t manage to make it work. I have the following “issue”.
    On our website, 1 page can have different URL’s. For instance, our “dog food” page can be approachaple via:
    https://www.example.com/dog/food
    https://www.example.com/food/dog

    If we want to work with the data in Google Analytics, this can a bit messy. Therefore, we also send a canonical URL for each page in our datalayer. The canonical URL in the example above is https://www.example.com/dog/food.

    In GTM I made a variable for this Canonical URL. Via a dimension I send this Canonical URL to Google Analyitics. This works fine. In Google Analytics I can see the Canonical URL as a dimension. But the problem is, that the whole Canonical URL is send to Google Analyics, including our domain name. My wish is to only send the URL after our domain name to GA: /dog/food

    To make this happen, I tried to make a Lookup Table variable in GTM, with the following settings:

    Input Variable:
    {{DLV – Canonical URL}}

    Lookup Table
    Input: Output
    {{DLV Canonical URL}} {{Page Path}}

    But this doesnt work fine. Now I see the original URL in GA, instead of the canonincal URL. Can you help me “converting” these URL’s? Im looking forward to a solution for this.
    Sander

  • Marin says:

    Hi Eoghan,

    is there a way to use this but still have a descriptive campaign name? Or would you use this and maybe add a lookup table for ad content that would be the same as the page title?

    Thanks,

  • Alexey Vorobyov says:

    Hello Eoghan from 2020 August

    And thanks for the article. Currently, there is an option in GTM to use RegEx to match the value both for simple strings and for lookup tables, which is great.

    • Eoghan Henn says:

      Hi Alexey,

      Thank you very much for your comment. When I first wrote this article, regex tables didn’t exist yet, but you’re right that it’s more than time to write an update 🙂

  • Fridtjof says:

    Hi Eoghan,

    A lot of the image files on this blog post are gone…

  • Leonidas Lainis says:

    Hi Eoghan and thank you for the article.

    I have created a lookup table with inputs three different domains (.com, .dk, .se; different languages of the same website) and outputs their G.A. tracking IDs. I did so, because in our CMS platform there is place for only one GTM container, so I had to combine all three domains in the same container, but avoid cross-tracking (I want to use different G.A. properties).

    Can I use this lookup table for Google Ads tag? If yes how? The G.A. tag in GTM needs a tracking ID, where I have used the lookup table. But the Ads tag needs a conversion ID and a conversion label…

    • Eoghan Henn says:

      Hi Leonidas,

      Yes, you can use the same structure for Google Ads, but you’ll need two lookup tables: One for the conversion ID and another one for the conversion label.

      Variable name: Conversion ID
      Variable type: Lookup table
      Input variable: Page Hostname
      Domain 1 > Conversion ID 1
      Domain 2 > Conversion ID 2
      Domain 3 > Conversion ID 3

      Variable name: Conversion label
      Variable type: Lookup table
      Input variable: Page Hostname
      Domain 1 > Conversion label 1
      Domain 2 > Conversion label 2
      Domain 3 > Conversion label 3

      I hope this helps! Please let me know if you have any further questions.

      Best regards,

      Eoghan

      • Leonidas Lainis says:

        Hi Eoghan and thank you for your response and help. Of course, it makes absolute sense. I did it already. Thank you again.

  • Scott Clark says:

    Excellent article. Have you ever seen lookup tables used to do things like swap out Google Adwords conversion tracking scripts or perhaps call tracking scripts? Some of these reside in the head and some in the body of the associated pages. I’m looking at improving efficiency for managing multi-location franchise landing pages and sites.

    • Eoghan Henn says:

      Hi Scott! Thanks for your comment. I’m happy you liked the article. I haven’t seen lookup tables being used for Google AdWords conversion or call tracking scripts, but it sounds like a good idea. I guess you could use lookup tables to match URLs with tracking IDs or conversion labels, in order to reduce the number of tags you need. Let me know how this goes for you if you decide to set it up and feel free to get in touch if you have any further questions.

  • Laura says:

    Really useful post. I plan to start implementing this right away 🙂 Can we use a tracking hashtag at the end of a PDF link, and it will still work the same? For example, use the following URL https://www.mywebsite/wp-content/uploads/2016/01/blog.pdf#cd122 That way I can track how people accessed the PDF.

    • Eoghan Henn says:

      Hi Laura,

      Thanks a lot for your comment. I’m glad you liked the post.

      I’m afraid the URL fragment tracking method won’t work like that with PDFs. PDF files don’t normally have the Google Tag Manager code, so the Google Analytics tracking code doesn’t fire on them either. There are however ways of tracking PDF views as page views in Google Analytics. Attention: Most guides you find when you search for this only explain how to track clicks on internal links to PDFs, but this will not help you in your case. You will need a solution like this one explained by LunaMetrics: http://www.lunametrics.com/blog/2013/06/04/tracking-pdfs-google-analytics-server-side/

      Once you’ve set this up, you can probably combine it with a URL fragment solution, but it will be a very different setup from the GTM solution described above.

      I hope this helps! Please let me know if you have any further questions.

  • Amir says:

    Hi,

    Any idea how i can upload multiple rows at once to a lookup table?
    I tried the method explained in the LunaMetrics blog (http://www.lunametrics.com/blog/2014/11/20/gtm-lookup-table-upload/) but it doesn’t work for me.

    thx
    Amir

    • Eoghan Henn says:

      Hey Amir,

      Have you seen the update from September 2016 in the article you linked to? Apparently the sheet wasn’t working for a while but now it’s fixed. Maybe this solves your problem already?

      Also, check out the article on the Seer Interactive blog they refer to (http://www.seerinteractive.com/blog/gtm-match-table-generator/). Very useful!

      Let me know if there is anything else I can do for you!

      Eoghan

  • Les says:

    Hello Eoghan: Thanks for your input and detailed response. I had missed Simo’s post in my research. I don’t think either solution will work for me as the URL structure I am working with is quite complicated. We are working with URL strings that are the result of a facetted search appliance. So you can imagine how messy things get!

    e.g. https://www.mysite.com/portfolio?keywords=producttype&f%5B0%5D=field_facet_network_type%3A8336

    In this case, I’d like to grab ‘8336’ and output the actual product type. I have a matrix that matches up the numbers we get in facetted search with the actual product type. The hope was to do this in GTM so that the actual product type would come out the other end in GA.

    My other option is to take the raw data from GA and do some Excel magic. Right now that seems like the best approach.

    As an aside, the example I provided is basic as compared to what one gets once you start drilling down one’s search.

    I really appreciate your time and the effort you put into your response. 🙂

    • Eoghan Henn says:

      Hi Les! Sure sounds messy. Good luck with finding a solution! And if you do come up with a way of doing it in GTM, let us know 😉

      Eoghan

  • Les says:

    Good post Eoghan.

    So what about a situation where you you want to output a certain value when a url contains certain characters? e.g> http://www.mysite.com/5346-good-deal and I want to output “Good Deal” based on the fact the the url contains ‘5346’

    • Eoghan Henn says:

      Hi Les!

      Unfortunately, GTM lookup tables only support one match type: “exact”. You need “contains” or “RegEx” so in order to achieve what you are looking for, you will have to implement a bit more than a lookup table. There are two solutions I can think of:

      First, I would like to refer you to Simo Ahava’s Simple RegEx Table for Google Tag Manager: http://www.simoahava.com/analytics/simple-regex-lookup-table-for-google-tag-manager/

      If you manage to implement this, it should do the job.

      Second, there is another solution I have used in a similar case that does involve the GTM lookup table macro and a tiny little bit of JavaScript. Here we go:

      If your URLs always start with a four digit number like the one in your example, you can use a very simple script to extract this string and then use it as input value for a GTM lookup table.

      Here’s the script:

      function() {
      var str = {{Page Path}};
      return str.substring(1, 5);
      }

      This will return characters 2 to 5 of your “Page Path” macro (so the four digit number in your example above). You just have to paste this script into a custom JavaScript variable in GTM and then use this new macro as input for your lookup table macro. In the lookup table, you can then match your four digit numbers with the output values you need.

      Please let me know if one of these solutions works for you or if you have further questions (sorry about my very brief description of the second solution).

      Thanks for your comment!

      Eoghan

  • Joao says:

    Hi Eoghan,

    I’ve been wondering, does lookup table suports regex?

    Like a radiobutton giving me numbers instead of names. and sometimes different numbers means the same thing.

    Something like this
    1|4|7 = Mastercard
    2|5 = Visa
    and so on (Is not working this way)

    Or i just have to do it like this

    1 = Mastercard
    2 = Visa
    4 = Mastercard
    5 = Visa
    7 = Mastercard

    Thanks in advance.

    • Eoghan Henn says:

      Hello Joao,

      Unfortunately, lookup tables do not support regex. The only available matching option is “equals”. But who knows, Google might introduce this future some time in future. I think it would be pretty useful.

      Anyhow, to answer your question: You will have to go with your secound idea (one line per input value).

      Talk soon,

      Eoghan

  • Heather says:

    Eoghan,

    Thanks for the awesome post! I was able to get this implemented and it is exactly what I am looking for.

    Question for you – I have lookup tables set for Source and Medium for social sites (fb, tw, li, gp) however I want to be able to customize the Campaign and Content on a per-URL basis.
    When i type in mysite.ca/#tw, I can see the source and medium in the real time report, however when I try mysite.ca/#tw?utm_campaign=campaignTest, the traffic comes through as direct.

    Alternatively, would it be possible to have 2 url fragments in one URL? something like mysite.ca/#tw#wintercampaign

    Thanks in advance for your help

    • Eoghan Henn says:

      Hello Heather,

      Thanks a lot for your interesting questions.

      Unfortunately, URLs like mysite.ca/#tw?utm_campaign=campaignTest will not work with lookup tables, because GTM interprets everything that comes after the hash (#) as the URL fragment. So the URL fragment in this case would be “tw?utm_campaign=campaignTest”. As the matching option of lookup tables in GTM is always “equals” (and not “contains”), the lookup table variable will have no output in this case. That’s why your traffic shows as direct.

      There is a workaround for this. Instead of a lookup table you can set up a trigger for each URL fragment and use it for a separate Universal Analytics tag that sends the first page view of the session with the corresponding values for source and medium. Just use the same trigger as excluding trigger for your normal page view tag that sends all other page views to GA. I have implemented this method for a client that uses URL fragments for affiliate pages that append their own tracking parameters to the URLs that already hav URL fragments and it works amazingly well.

      However, I would not recommend using utm_campaign parameters without their triplet siblings utm_source and utm_medium. These three should alwys be used together and not separately.

      Your second idea of using two URL fragments like mysite.ca/#tw#wintercampaign will not work either, because every URL can only have one URL fragment. It is important to keep this in mind when using the URL fragment solution for tracking traffic sources, as sometimes your destination URLs will already have URL fragments for other purposes. For example, your page might use URL fragments in anchor link URLs, such as mysite.ca/article-xyz/#chapter-1. In this case you can’t use a URL fragment to track traffic to mysite.ca/article-xyz/#chapter-1#tw.

      To solve your problem, I would suggest you create URL fragment combinations of source, medium, campaign and content, just like I explained to Vert in a previous comment. You could use fragments like this:

      #tw1a
      #tw1b
      #tw2a
      #tw2b

      and so on…

      It’s not too much hassle to go to GTM before publishing a new link and adding some values to your lookup tables.

      I hope this helps! Please let me know if you have any additional questions.

      Eoghan

  • George says:

    Hi Eoghan,
    Would this method work on a site which uses iframes – where the GTM is placed on the iframe itself.

    Many thanks,
    George

    • Eoghan Henn says:

      Hello George,

      Yes, alll of this will also work on a website that uses iframes. When you track things that happen in iframes, it is important that you include the GTM code in the documents that are loaded in the iframes and if these documents are hosted on different domains, you need cross domain tracking. Otherwise, Google Analytics would start a new session every time something happens on a different domain.

      If you have any additional questions on this, please just let me know!

      Eoghan

  • Vert says:

    Dear Eoghan,

    thank you for the great article! I stumbled upon the same Lunametric article and was interested in using the method for a cleaner UTM URL. I’d like to have my link as something like “www.mywebsite.com/#1” while it would be sent to GA as “www.mywebsite.com?utm_source=testsource&utm_medium=testmedium&utm_campaign=testcampaign” instead. I’m having difficulties implementing this, though. How should I set up my lookup table in GTM? I appreciate any pointers I can get. Thank you and keep up the good work!

    • Eoghan Henn says:

      Hello Vert,

      Thanks for your kind words.

      You will need three lookup tables to set this up, one for the dimension “source”, one for “medium”, and one for “campaign”. You then choose the URL fragment as input variable for each of the lookup tables and match it with the values for source, medium and campaign you want to send to GA.

      So your value pairs in the source lookup table would look like this:

      1 – testsource1
      2 – testsource2
      3 – testsource3
      4 – testsource4
      and so on…

      For the medium lookup table it would be this:

      1 – testmedium1
      2 – testmedium2
      3 – testmedium3
      4 – testmedium4
      and so on…

      And for campaign it would look like this:

      1 – testcampaign1
      2 – testcampaign2
      3 – testcampaign3
      4 – testcampiagn4
      and so on…

      Now how do you get the output of your lookup tables into GA?

      In your normal page view tag in GTM, you go to More settings > Fields to set and you define the following three fields:

      – campaignSource
      – campaignMedium
      – campaignName

      Then you populate the three fields with the output values of the three corresponding lookup tables. The values will now be sent to GA just as if you were using the UTM tracking parameters utm_source, utm_medium and utm_campaign.

      I hope this helps! Let me know if anything remains unclear.

      Best regards,

      Eoghan

      • Vert says:

        Thank you, Eoghan! I was trying to fit all 3 parameters into 1 lookup table so that might be where I screwed up… I will give your method a shot and report back! It’s so very kind of you to take the time to answer a newbie question like mine! 🙂

      • Manu Kivila says:

        Hi,

        I just tried to implement something similar but couldn’t see anything (source didn’t change and it was still “direct / none) in real-time reports.

        Trying to implement tracking for instagram: abc.com/#instagram

        Here’s my implementation (normal pageview tag): http://prntscr.com/b99hdf
        Real-time report: http://prntscr.com/b99j5w
        Variables: http://prntscr.com/b99jgc
        Lookup table: http://prntscr.com/b99kkx

        URL fragment is set as you showed.

        What would be wrong with the implementation? Why it doesn’t show up in real-time reports?

        • Eoghan Henn says:

          Hi Manu,

          Thanks for your comment. From what I can see, your implementation looks fine. Here’s what I would recommend:

          • If the real-time report doesn’t show the results you expect, but you can’t find any mistakes in your implementation, always check your normal reports after a few hours. The real-time report doesn’t always work properly and often shows different data from the final reports.
          • In GTM debugging mode, go to your page view tag and check in the properties under “Fields to set” if campaignSource is listed and has the desired output.
          • If you still find that it’s not working and you implemented everything correctly, you might want to try to send a value for campaignMedium too. I’ve never tested this, but I imagine that GA might have problems if you only submit a custom value for campaignSource, but not for campaignMedium. After all, these two dimensions are tied together very tightly.
          • I hope this helps. Let me know how it goes!

            Eoghan

          • Eoghan Henn says:

            P.s.: I did just test that third thing I mentioned while I was writing the comment and when you only pass campaignSource, the dimension “Medium” shows as “(not set)” in the final reports. In the real-time report, however, my visits showed as “(direct) / (none)”. So I guess that’s what you were looking for! Does your first test show as “instagram / (not set)” in “Source/Medium”?