• GTM Courses
  • Blog
  • Services
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
  • About
    • About
    • Contact
  • GTM Courses
  • Blog
  • Services
  • Resources
    • Youtube channel
    • E-books and Guides
    • GTM Recipes
    • View All Resources
    • GTM Community
  • About
    • About
    • Contact

May 21, 2020

What is URL variable in Google Tag Manager?

Updated: May 21th, 2020. Have you ever been in a situation when you need to extract a part of the URL and turn it into a variable in Google Tag Manager? I have. The most common demand is to transfer a so-called query string parameters to GTM.

For example, I have a web address https://www.example.com/?product_id=123&category=shoes. Believe it or not, but there’s a very easy way to turn product_id and category data into variables. I’ll show you how.

The magic lies within a user-defined variable called URL. In Google Tag Manager interface, go to Variables > User-defined Variables > New  and choose URL.

 

Video tutorial

Do you prefer video content to text? If yes, then take a look at the video below. But keep in mind that the blog post is more in-depth.

 

What does the URL variable do?

The URL Variable can be used to access components of the current page URL. This is a very versatile Variable type and is especially useful for picking query parameters and hash fragments in your URLs. Note: There are Built-In Google Tag Manager Variables for some situations (i.e. Page URL, Page Hostname, Page Path).

Let’s say the address of the web page that I am currently on is https://www.example.com/welcome:8080?gclid=aabbcc123#home.

Now, I will describe every component type that is available in the URL variable:

  • Full URL – returns the full URL without the hash fragment, e.g. https://www.example.com/welcome?gclid=aabbcc123.
  • Protocol – returns the protocol of the URL, e.g. https.
  • Host Name – returns the hostname of the URL without the port number, e.g. www.example.com. You can choose to Strip ‘www.’ to strip the ‘www’ subdomain from the hostname.
  • Port – returns the port number used in the URL. In my example, the variable will return the 8080 value. Or 80 for HTTP / 443 for HTTPS, if the URL had no port number.
  • Path – returns only the pathname in the URL (in other words – everything after the hostname, except port, fragment, and query). In my example, I would get /welcome/ value. You can also specify Default Pages to strip pages with names like ‘index.html’ or ‘index.php’ from the return string.
  • Filename extension – if a URL looks like this https://www.yourwebsite.com/download/filename.pdf, the Filename extension will return the value “pdf”. This might be useful if you want to get the filename extension of a clicked link.
  • Query – returns the entire query parameter string (without leading ‘?’), if you don’t specify a query key. In my case I could specify query key to gclid, thus URL variable’s value would be aabbcc123. If you do specify a query key, but that key is absent, the variable’s value would be undefined.
  • Fragment – returns the value of the URL’s fragment without the leading ‘#’, in my case value would be home.
  • You can expand the More Settings tab to find a source selector. In this selector, you can choose the Variable whose return value the URL Variable will access.

URL variable in action

Here’s a quick example I came up with.

If you’re using an email marketing tool, you’ve probably already noticed that after a recipient clicks a link in your email, he/she is redirected to your website. The link contains a lot of parameters after a question mark, e.g. https://www.mybusiness.com/?utm_campaign=awesomecampaign&utm_source=campaignid&utm_medium=email.

These 3 query keys are called UTM parameters. They are especially useful when it comes to tracking campaigns in Google Analytics.

You can easily turn utm_campaign, utm_source and utm_medium into GTM variables and then utilize them in triggers or tags. Just create three variables with the following settings:

After you have created these three variables, enable Preview and Debug mode in GTM and head to the website where you’re working on. At the end of URL add ?utm_medium=test1&utm_source=test2&utm_campaign=test3. If the URL you’re working on already has some query (it starts with a question mark), then just add &utm_medium=test1&utm_source=test2&utm_campaign=test3 to the end of the link and hit enter.

The page will refresh, GTM Preview and Debug console will load. Click on any event (e.g. Page view) and open Variables tab. The result should look like this:

3 UTM variables in GTM

That’s it!

If you want to take a look at some use cases where this variable is useful take a look at these guides:

  • Extract part of the URL
  • Transfer UTM parameters from one domain to another

 

Want to extract part of the URL path?

Even though the URL Variable in Google Tag Manager is definitely awesome, it still lacks some features. For example, you cannot extract the part of the URL path.

Let me illustrate: the URL of the page is www.example.com/products/category/shoes/nike-lorem-ipsum. What if you wanted to use the value of the category (in this case, it’s “shoes”) in your tags?

While this is currently not possible with the URL variable, you can do this with a Custom JavaScript variable.

 

Conclusion

In this blog post, I have described a pretty easy way how to extract a section of URL and turn it into a variable that can be reused in other tags and variables. Do not hesitate and start utilizing the URL variable in your web analytics today.

Julius Fedorovicius
In Google Tag Manager Tips
19 COMMENTS
rohini
  • Aug 2 2017
  • Reply

Hi..
I found your article really interested. I am currently facing an issue with the utm_campaign. If i follow your article then i wont be able to see the campaign data in acquisition-> campaign report. Could you please help me finding solution for my problem. In my url: utm_campaign= [email protected] is coming, so i need to pick only utm_campaign as "test_cq". could you please suggest.

    Julius Fedorovicius
    • Aug 3 2017
    • Reply

    Hey, there are several issues which need to be addressed here:

    1. I'm pretty sure there should be a "&" sign between utm_campaign=test_cq_ AND [email protected]
      As soon as the link's query parameters are equal to [email protected], GA will automatically fetch utm_campaign and no additional configuration will be needed. You should contact developers who generated such link to fix the issue.
    2. The fraction of the link you provided contains an email address. When a visitor lands on your page of which URL contains an email address, that URL is stored in Google's servers. This violates Google Analytics Terms of Use. You cannot store personally identifiable information in Google Analytics severs
Kristi
  • Nov 12 2017
  • Reply

Can this be used to pass the utm parameters string from page to page? Wordpress is currently stripping out the utm parameters after I click away from the landing page.

    Julius Fed
    • Nov 13 2017
    • Reply

    Hey, why would you like to keep UTM parameters on every page? Once the visitor lands on a page, Google Analytics fetches all UTM parameters and sets it for the entire session.

    You do not need to keep those parameters on every page.

      Kristi
      • Nov 13 2017
      • Reply

      Julius,

      I am wanting to send the information to my CRM via my lead form page after they come i. I'm using infusionsoft.

      PPC > Landing Page > Lead Form Page > Infusionsoft.

      Thanks.

        Kristi
        • Nov 13 2017
        • Reply

        Thank you for your answer! I've been able to make it work with the help of itracker360's plugin.

          Julius Fed
          • Nov 13 2017

          I'm glad you found a solution. Actually, you gave me an idea for the next blog post :)

      adam rowe
      • Dec 28 2017
      • Reply

      once the UTM parameters are set, how can you access them?

        Julius Fed
        • Dec 29 2017
        • Reply

        Hey, Once the UTM parameters are set in the URL, you should create 3 URL variables in GTM with the following settings:

        Component type: Query
        Query key: utm_medium

        Component type: Query
        Query key: utm_source

        Component type: Query
        Query key: utm_campaign

        Once these variables are created, you can use them in tags, triggers and other variables.

          adam rowe
          • Dec 29 2017

          Right, but this is in reference to what I think Kristi above was trying to do: this is all great for the initial page visit, but what if someone for example doesn't fill out a form until they've been around the site for a minute or two? It would be nice to pass the original utm_variables on with the form, since it's a no-no to keep PII in GA.

          Julius Fed
          • Dec 29 2017

          Then you might find this blog post useful https://www.analyticsmania.com/post/transfer-utm-parameters-google-tag-manager/

          Julius Fed
          • Dec 29 2017

          Or you can save those UTM values in cookies with help of GTM. Here's a guide how to set and read cookies - https://www.analyticsmania.com/post/cookies-with-google-tag-manager/

      Jacky Dwane
      • Sep 24 2020
      • Reply

      Hi Julius,

      My question is in relation to your wonderful article: https://www.analyticsmania.com/post/transfer-utm-parameters-google-tag-manager/ article, but is along these lines above as well. We can't currently track the UTM parameters associated with the submission of the form to our Salesforce instance (doesn't carry them on). I've implemented your solution in the article above, but the landing page from the Google ad with the UTM isn't the form page, and drops when the form submits to Salesforce.

      From the preview, I can see it holds the variables on the first page, but not subsequent pages. I think other people have asked you variations of this, but not to Salesforce, and not if the user clicks around the section of the site a bit before the conversion (hitting submit on the form).

      Is this possible, or do we need some other kind of clever solution that only a developer could implement?

      So, the example funnel is:

      Link on Google paid advertising (with UTM source/medium/campaign > example.com/page1 > example.com/page2 >example.com/page3>example.com/form> Salesforce > example.com/submissionsuccesspage.

      If you can help, I'd really appreciate it, even if it's just pointing me in the right direction with a few wise words.

Chris Lamothe
  • Jan 10 2018
  • Reply

What about getting a piece of a url, for example example.com/cart/add/xxxxxxx/ if I just wanted to get xxxxxxx and turn that into a variable such as productID or similar.

    Julius Fed
    • Jan 10 2018
    • Reply

    Hey Chris, you should use Custom JavaScript variable then and enter this code:

    function () {
    var urlPart = document.location.pathname.split('/')[3];
    return urlPart; }

    It splits Page Path with / . As a result, you get an array containing 5 items:
    - "" (empty string)
    - "cart"
    - "add"
    - "xxxxxx"
    - "" (empty string)

    Then you need to choose the 4th item in the array. In JavaScript, counting starts from 0, that's why I used [3] in the script. Hope this helps.

Yuval
  • Jul 11 2020
  • Reply

Is there a way use Tag Manager to keep URL parameters (namely Google Ads IDs) across a site?
That's in order to capture those parameters in different ways. For example, add these IDs to a form submission.
Thanks!

    Julius Fedorovicius
    • Jul 13 2020
    • Reply

    Store them in cookies https://www.analyticsmania.com/post/cookies-with-google-tag-manager/

      Yuval
      • Jul 13 2020
      • Reply

      Thank you! I'll have a go at it :)

Ioannis
  • Nov 16 2020
  • Reply

Hi Julius. Firstly, I would like to congratulate and thank you for all the help you are giving us all. Your effort is very complete and valued.

I would like to ask you what kind of tag I should create for the following case:

I have a directory and I would like to be able to have a report of users that visit every entry. A typical directory entry URL is as follows: https://chilesymaiz.com/es_mx/detalles/name/100-mexico/

I have created the following variable: Tiendas: Custom JavaScript: function() {
var pageUrl = window.location.href;
return pageUrl.split("/")[6];
}

And the following trigger:Vistas de Directorio: Page View: Some Page Views: Tiendas/contains/name

Can you please help me on the kind of tag I should create in order to be able to get a report in GA?

Thanks a million!

Leave a comment Cancel reply

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

 

Hi, I'm Julius Fedorovicius and I'm here to help you learn Google Tag Manager and GA (but mostly GTM). Join other 100 000+ monthly visits and 14000+ newsletter subscribers in this exciting journey. Read more
Essential resources


Popular articles
  • 🔥 GTM Form Tracking: 7 Effective Methods
  • 🔥 dataLayer.push: The Guide
  • 🔥 GTM vs Google Analytics
  • 🔥 99 Things You Can Do with GTM
  • 🔥 Common GTM Mistakes
  • 🔥 Data Layer: Ultimate Guide
  • 🔥 60+ Custom JavaScripts for GTM

Early-bird discount expires:

Days
Hrs
Min
Sec
Analytics Mania
  • Google Tag Manager Courses
  • Google Tag Manager Recipes
  • Google Tag Manager Resources
  • Google Tag Manager Community
  • Login to courses
Follow Analytics Mania
  • Subscribe to newsletter
  • RSS feed
Recent Posts
  • Track Contact Form 7 with Google Tag Manager
  • Missing DOM Ready and Window Loaded events in GTM?
  • The most up-to-date GTM course is here
Analytics Mania - Google Tag Manager and Google Analytics Blog | Privacy Policy
Manage Cookie Settings