Last Modified on November 1, 2024
Marketers, are you encoding parameters in session URLs with query strings to track user data? Sounds complicated—but it’s an easy and valuable way to track users.
If any of that sounded confusing or technical, don’t worry. This beginner-friendly guide will simplify query strings and parameters so that you can reap their benefits.
In this guide, we’ll learn how to use query strings to track parameters like campaign data, site searches, or cross-domain sessions.
An overview of what we’ll cover:
- What are query strings?
- Why query strings are important
- The anatomy of a query string: how it works
- Separating query strings into key-value pairs
- Using multiple values and symbols in a query string
- How marketers should use query strings
Let’s dive in!
What Are Query Strings?
For our purposes, query strings are tails added onto URLs that carry information and communicate with tracking tools to enhance your data.
To understand query strings, let’s first take a closer look at a URL that uses one.
The first section of a URL is called a protocol. It is followed by the domain name of the website, or the hostname. And the next part (after the forward slash) is the path.
Everything that follows the path after the question mark (?) is the tail of the URL, also known as the query string.
But what do these components do?
As you know, a URL is an address that takes you to a specific webpage on the internet. Each component of the URL helps us do that by performing different functions.
For example, the protocol determines the communication method between your browser and the server. This simply means how your computer (the “client”) requests and receives information from the web host (the “server”).
The hostname or domain identifies which server you are contacting. The whole website is usually contained on that one domain, but contains several webpages. The path leads you to specific folders and resources inside the server to display the specific information for a webpage.
So if you modify any of the above components, you (the user) will most likely end up on another page.
But the query string is just the tail of the URL. Even if you omit the tail, the web address would still remain the same. Since the protocol, hostname, and path are the same, you would still land on the same webpage.
What tells the server that part of the URL is a query string instead of an extension of the path?
Query strings always begin with a question mark—pretty convenient solution.
So if the query string can be omitted without any changes, why do we still use it in our URLs?
Why Query Strings Are Important
The query string may not provide any information to the servers, but it gives valuable information to other tools such as Google Analytics.
There is a feature on Google Analytics known as the UTM Tracking that enables us to classify traffic based on the source of the traffic. (UTMs are a particular kind of query string—more on that later.)
In other words, UTM tracking tells you exactly where a user was right before they navigated to your site. This is really useful for determining how successful certain ad campaigns and backlinks are.
UTM tracking shows us these results in the Source/Medium reports.
There are tons of ways to use UTM parameters. You might have even seen cases where people change or manipulate default UTM parameters to track different types of data, depending on their tracking needs.
But to understand UTMs, which are a special type of query string, it’s important to first understand the different parts of a query string and how they function.
Query Strings Applications
- Web Page Navigation: Query strings can modify page content based on user preferences, such as filtering, reordering, paginating, and more.
- Landing Page Personalization: They can dynamically populate content to create variations of your landing page based on user search queries.
- Cross-Domain Tracking: For tracking user interactions across multiple related domains.
- Campaign Tracking: Using UTM parameters, marketers can track various attributes of their traffic within tools like Google Analytics.
- Forms Population: They can prepopulate billing fields and other information automatically, enhancing the user experience.
- Email Marketing: Personalize emails by passing variables dynamically through query strings.
- Affiliate and Referral Program Support: Create custom links for partners and customers to track and reward referrals.
- Discount Codes: Automatically apply discounts to a customer’s shopping cart through query strings.
- Lead Qualification: Assess the quality of leads by tracking user interactions on specific pages.
The Anatomy of a Query String – How It Works
I’m going to break down an entire URL in a Google Sheet to show you how query strings are built and what all the parts mean.
The query string, as we already know, will be the entire tail of the URL after the path, including the question mark ( ? ).
The first part of the query string is called the identifier. An identifier marks the beginning of a query string. In a URL, the identifier is always a question mark ( ? ).
The next part is the connector. A connector, well, connects the different key-value pairs. Connectors are usually marked by the (&) sign.
The last components are key-value pairs. These are pairs of words connected by an equals sign ( = ) in which the first word describes a parameter and the second describes the value (explained more in the next section).
Key-value pairs contain the information that we want to send to our tracking tools like Google Analytics; they’re also known as UTM parameters.
You’ll find the key-value pairs between the identifier and the connector. Thus, in our example below, we can identify three such key-value pairs: utm_source=newsletter, utm_medium=email, and utm_campaign=summer15.
Let’s separate them out to understand them better.
Separating Query Strings into Key-Value Pairs
A key-value pair can be separated into a key, a value, and a separator.
The separator is the equals sign ( = ). The key comes before the separator, and the value comes after.
So, if our key-value pair is utm_source=newsletter, then our key will be utm_source and our value will be newsletter.
Simple, right? Each key-value pair in a query string will be unique.
These key-value pairs make up the information that is coded inside our query string. Once you know how to read this data, it becomes very easy to interpret the Google Analytics reports that use UTM parameters.
For example, in the Source/Medium report of Google Analytics, the source would be a newsletter—which means that the user navigated to your website from a link in your newsletter.
If the medium is “emailing,” then it means that the user came from a source that you emailed to them; and “summer15” describes which advertising campaign was associated with the source.
Starting to make sense?
To see how this data point looks in Google Analytics, let’s see the report for a pageview with a query string in the URL. In the Real-Time reports of Google Analytics under Traffic Sources, you’ll find that the new visitor came from the Medium email and the Source newsletter.
You can also click on this entry to know the Campaign of the traffic. In our case, it will be summer15.
This is one of the many use cases of a query string in a URL. By adding UTM parameters to URLs that you distribute via your marketing campaigns, you can ask tracking tools where your traffic has come from. This helps you determine campaign success and track your sales funnel.
Similarly, you can use query strings for Facebook campaigns, ad campaigns, etc.
Bear in mind that the key-value pairs that you add in the URL need to be URL encoded. This can be especially tricky in the case of multiple values, so let me demonstrate how to do this.
Using Multiple Values and Symbols in a Query String
While adding multiple values or symbols in your query string, you need to encode them correctly in order for your query string to work.
URLs are limited in the types of characters you can use and how you can use them. Plus, some characters are reserved for specific functions (like how the question mark generates a query string).
If you use certain symbols or characters that are not strictly alphanumeric, you will need to use special codes for them to appear correctly in your reports.
For example, the space between two words is encoded with a plus ( + ) sign.
Similarly, a question mark ( ? ), an equals sign ( = ), or an umlaut ( ü ) will also need to be encoded.
The best and the easiest method to encode your parameters is to use a URL Decoder/Encoder (I use this decoder from meyerweb.com). All you have to do is enter the string that you want to encode and click on the Encode button.
The tool will return an equivalent encoded string that you can use as your query string.
Now that we know what a query string is and how it works, let’s see how you can use them to your advantage as a marketer!
How Marketers Should Use Query Strings
One of the uses of query strings is to communicate with a server. It does this by sending information back and forth.
Additionally, you can use query strings in marketing tools. In the previous example, we learned how to use query strings with UTM parameters to explicitly ask Google Analytics to sort traffic based on its sources.
If you’re new to using UTMs, we created a UTM Tracking Tool to help you build your own query strings. You can directly input the campaign, medium, and source values that you want to track in the spreadsheet, and the tool will automatically generate a URL for you. Pretty cool, right?
But the use cases of query strings do not end here!
Cross-Domain Tracking
If you’re linking your website to another related website and want to measure the user on both the sites as a single user, you can use query strings for cross-domain tracking.
To demonstrate this, let’s click on the login link on our website.
This link will redirect you to another page. We’ve set up our link so that it attaches a query string with a huge ID number when it directs you to the MeasureMasters domain.
This particular query string helps Google Analytics recognize the second domain session as part of the original session on the first domain. Using the ID number generated by the link click and attached to the new URL, it will combine these two sessions into one.
If you don’t have cross-domain tracking set up, Google Analytics will see this as separate traffic—which makes it difficult to see how users are interacting with you across multiple related domains.
Site Search Tracking
Another use of query strings is to track site searches. This is especially helpful for tracking product demand on eCommerce websites.
For example, if you search for a keyword on our demo shop, it will redirect you to a page with a query string. The query string on this URL will have a key s and a value of whatever your search term was.
This key-value pair communicates search information to the server and displays relevant results on the website.
However, you can also use this query string in your Google Analytics account to track the terms searched on your eCommerce website.
To do this, add the key of the query string in the admin section under the View Settings of your account. The Query parameter will be s in this case.
Google Analytics will identify the search results on your website and track the values accordingly. You’ll find this report under Behaviour → Site Search → Overview.
Email Tracking
Lastly, we can customize emails or website greetings by passing variables dynamically through a query string.
Let’s demonstrate with an example.
You can add the first name of your subscriber using a variable in your emails. You can pass this same variable in a query string such that the link for each subscriber is customized with their names.
To do this, you’ll need to add a key-value pair where the key is first_name and the value will be the variable subscriber.first_name. Make sure that this is a dynamic value by including double curly brackets!
When the user gets the email, they will see their name in the email.
And when the user opens the customized link, it will feature their name in it.
You can also use this variable name in the query string to autofill a contact form on the website or to display personalised messages on the website.
These are some of the use cases of query strings. You can use them with different tools for different scenarios for your own marketing efforts.
FAQ
How do query strings work in a URL?
The query string is the portion of a URL that follows the question mark (?) after the path. It consists of key-value pairs connected by connectors (&). The key-value pairs contain information that is sent to tracking tools.
How can multiple values and symbols be used in a query string?
When using multiple values or symbols in a query string, they need to be properly encoded. Characters like spaces, question marks, equals signs, and umlauts require special encoding codes to appear correctly in reports.
How can cross-domain tracking be implemented using query strings?
Cross-domain tracking can be achieved by attaching a query string with a unique ID to the URL when redirecting users from one domain to another. This allows Google Analytics to recognize the second domain session as part of the original session on the first domain.
Summary
So that’s it! This is how you use query strings for your marketing campaigns.
Query strings essentially send information back and forth, either to a server or to an analytics tool. As a marketer, it can help you track the source of your traffic, monitor the terms searched on your website, or personalize your emails and website.
What are your favorite use cases for query strings? And how do you encode the URL? Let us know in the comments below!
Is UTM tracking considered as a cookieless solution ?