
What is URL variable in Google Tag Manager?

When working with Google Tag Manager (GTM), URL variables are essential for extracting specific information from web page URLs. GTM allows you to break down a URL into different components, which can be used for tracking, analytics, and marketing automation.
In this blog, we'll explain each URL component using a real-world example and how GTM helps extract useful data.
Example URL Breakdown
Let's take the following URL as an example:
https://www.easemytrip.com/holidays/holidays/Query?querynumber=QINDHOL02FEB250210&packDetail=UUlOREhPTDAyRkVCMjUwMjEwfDIvMi8yMDI1IDEyOjAwOjAwIEFNfEVNVCBFeGNsdXNpdmUgQmFsaSBGaXhlZCBEZXBhcnR1cmUgd2l0aCBHdWlkZWQgVG91cnN8fHRlc3RAZ21haWwuY29t
We will now break this down into its components:
1️⃣ Protocol
The protocol refers to the method used to access a web page, typically HTTP or HTTPS.
🔹 GTM Setting: Select Protocol
🔹 Extracted Value: https
2️⃣ Host Name
The host name is the main domain of the URL, excluding the protocol and port.
🔹 GTM Setting: Select Host Name
🔹 Extracted Value: www.easemytrip.com
✅ If you enable "Strip 'www.'" → It will return easemytrip.com.
3️⃣ Port
The port is a number that specifies the communication endpoint for the request. Default ports are:
- 80 for HTTP
- 443 for HTTPS
🔹 GTM Setting: Select Port
🔹 Extracted Value: Since no port is explicitly mentioned, GTM will return 443 (default for HTTPS).
4️⃣ Path
The path is everything that appears after the domain but before any query parameters (?).
🔹 GTM Setting: Select Path
🔹 Extracted Value: /holidays/holidays/Query
This is useful for tracking specific page visits.
5️⃣ Filename Extension
The filename extension extracts the file type if a downloadable file is included in the URL.
🔹 Example URL: https://www.example.com/download/document.pdf
🔹 Extracted Value: pdf
🔹 For Our URL: No file is present, so GTM returns an empty string ("").
6️⃣ Query Parameters
The query string consists of key-value pairs after the ? symbol, used for tracking and passing data.
🔹 GTM Setting: Select Query
🔹 Extracted Value:
querynumber=QINDHOL02FEB250210&packDetail=UUlOREhPTDAyRkVCMjUwMjEwfDIvMi8yMDI1...
✅ If you want only querynumber, set "Query Key" = querynumber.
🔹 Extracted Value: QINDHOL02FEB250210
This is useful for tracking specific campaign IDs, user sessions, etc.
7️⃣ Fragment (Anchor Tags)
A fragment is the part of the URL after the #, used for in-page navigation.
🔹 Example URL: https://www.easemytrip.com/holidays#home
🔹 Extracted Value: home
🔹 For Our URL: Since no fragment is present, GTM returns an empty string ("").
Final Summary Table
GTM Variable Component
Extracted Value
Protocol
https
Host Name
www.easemytrip.com
Port
443 (default for HTTPS)
Path
/holidays/holidays/Query
Filename Extension
(empty)
Query
querynumber=QINDHOL02FEB250210&packDetail=...
Query (Key: querynumber)
QINDHOL02FEB250210
Fragment
(empty)
How to Use URL Variables in GTM?
1️⃣ Go to GTM → Variables → New → Select URL Variable.
2️⃣ Choose the desired component (e.g., Query, Path, Hostname).
3️⃣ For Query Parameters, specify the exact key (e.g., querynumber).
4️⃣ Save the variable and use it in your triggers, tags, or analytics setup.
Conclusion
URL variables in Google Tag Manager provide a powerful way to extract and utilize different components of a URL for tracking, analytics, and automation. By understanding how to configure GTM variables correctly, you can improve your campaign tracking, capture user journey data, and enhance website performance analytics.