Home » Blog » Data » How to Use URL Fragments & Query Strings as Custom Dimensions in Google Analytics

How to Use URL Fragments & Query Strings as Custom Dimensions in Google Analytics


What are UTM Parameters and Why Do They Matter?

UTM Parameters are great aren’t they? First of all, who doesn’t love the word Urchin? Sounds kind of like the combination of a burp and an itch. But seriously, UTM’s have opened up the ability to track your ROI with Google Analytics. For those of you who don’t know what a UTM Parameter is, it’s a series query string appended to the end of a URL which sends data to different analytics platforms. You may have seen a URL that looks like this after clicking a link on Google or Facebook: cleanurl.com/?campaignId=6L9UY&mcid=Facebook&mc=paid_social&subid=USGM&subid1=mktg_ This URL is sending all kinds of data into whatever analytics platform cleanurl.com is using and letting it know that this link was clicked on the Facebook platform, that it was from a paid social campaign, and even what ad and type was clicked. This type of tracking is important because if I were to buy from this link, the information could attribute my sale to the click. If the click cost money, cleanurl.com would be to calculate their marketing spend and return for the sale.

What are the Drawbacks of Using UTM Parameters?

There are a couple of issues with UTM parameters:

  1. The obvious is that they look disgusting. You would never want to post them in a forum, give them to a customer, or put them on print as they are long and look terrible. You can get around this by creating a shortlink out of the UTM but these aren’t usually seen as trustworthy links.
  2. UTMs are technical and take effort to build. If you have a sales staff and want to track their activity while sending personalized emails to customers, good luck teaching them how to use UTMs.
  3. Without proper lowercase filters, UTM parameters can make your analytics look unclean. They need to be carefully organized or else you’ll get a handful of dirty data.

So what’s the solution? Either use a simple URL query string or a URL fragment and push them into Google Analytics as a custom dimension. I will review both options in this tutorial.

How to Setup a Custom Dimension in Google Analytics

First, it would be good to go over how to setup a custom dimension in Google Analytics. Whichever option you end up going with, the first part of the setup is the same. Navigate to your Google Analytics (analytics.google.com) and go into the admin portal. Underneath the property column towards the bottom you should see a section called “Custom Definitions.” Click on that and select custom dimensions. If you’ve never set one up before, you should only see a button that says +New Custom Dimension. Click on that button and you can create your custom dimension. We are not going to get into scope definitions on this tutorial, but you can read up about it here: https://support.google.com/analytics/answer/2709828#example-hit

Campaigns for the most part have a session based scope, as you want to attribute data to whatever link they click from a campaign at any given time. This would only change if you want to capture whatever link they FIRST clicked on, in which case it would be user scoped. Regardless, we’re going to go for a session based scope. Put in whatever you’d like here, but think about the overall purpose of the campaign since it can really be anything. This is merely a label so you can find it later, and the values will be dynamic. Save your custom dimension, ignore the code, and that’s it! Now we need to decide what we want to track.

Using URL Fragments as a Custom Dimension

URL Fragments are hashtags abridged to the end of a URL with text after them. Often times you’ll see them on web pages that have links that click down to other parts of the page (click ID’s or bookmarks). They look like this: https://www.greatbigdigitalagency.com/#funky If you were to click on that now, it would just go to our normal homepage. But we can use that  fragment to communicate information to analytics. For this example, let’s say that you had a sales team and you wanted an easy way to identify which sales team member is pulling in the most sales. Having them use UTM parameters would be a nightmare, as it puts little control in your hand, has a wide margin of error, and looks really ugly in an email going out to a potential customer. Which looks better: https://www.greatbigdigitalagency.com?utm_source=mr%20salesman&utm_campaign=promotion&utm_medium=email Or https://www.greatbigdigitalagency.com/#1? Yeah we know, the latter one. Well we can use that number to pass information into Google Analytics. So what we’re going to need for this recipe is Google Tag Manager, a URL variable, a lookup table, and a Google Analytics Tag. Sounds complicated, huh? The nice part is after the initial lift, you can easily add or subtract values. Let’s begin. Say you have 4 different events you want to track: Comic Con; Music Con; Film Con; and Fun Con. You can create fragmented URLs to send info to Google Analytics very simply like this: https://www.greatbigdigitalagency.com/#comiccon, https://www.greatbigdigitalagency.com/#musiccon, https://www.greatbigdigitalagency.com/#filmcon, https://www.greatbigdigitalagency.com/#funcon Now you have a quick and simple URL that looks harmless to pass to a potential customer at the event. We can now use Google Tag Manager to pass through valuable information to Google Analytics.

URL Fragment Variable

Log into your GTM interface and navigate to variables. Choose to create a new user-defined variable and select a URL variable.

This will automatically select whatever comes after a hashtag in a URL. From there you can use those values any way you’d like. In this case, we may be content to just send it as is to Google Analytics as our value. Fragment Lookup Table Say however, we want to make it a bit more descriptive. We can use a lookup table to change the values before sending it into Google Analytics.

Update your Universal Analytics Tag

From there we just need to update our main Universal Analytics Tag with the custom dimension information. Navigate to your Universal Analytics Tag. (If you don’t have one already, I highly suggest setting one up instead of having it inline on the page. I have a great resource on how to set this up here: How to setup Google Analytics through Google Tag Manager.) Select your Universal Analytics Tag and navigate down to custom dimensions underneath More Settings. Add the dimensions index number (which you can find in Google Analytics under your custom dimension section: see how to setup a custom dimension in google analytics,) and add the Fragment Lookup or Fragment URL variable you just created.

Voila, you should have a completely trackable URL dimension, similar to a UTM but without all the grossness. If you just used a regular fragment variable, this is completely dynamic and you can continue to create fragments and it should automatically capture in Google Analytics. If it’s a lookup table, you will need to add the values that you want to capture. I suggest this approach, as sometimes sites have smooth scroll, bookmarks, or other things that result in fragments. You could end up pulling in values that you don’t want if you leave this to be completely dynamic. Using the query method or the lookup table is a way to prevent this from occurring. But yay, you did it, give yourself a sodapop!

Using a URL Query as a Custom Dimension

This is very similar to the method above but instead of using a fragment under URL variable, you’d select query. A URL query is something that looks like this: https://www.greatbigdigitalagency.com/?event=comiccon This provides a little bit more dynamic flexibility than a fragment as it will take the value of anything after the equal sign and you provide GTM with the code. In this case, our code is “event.” We can put anything after “event=” and it can dynamically capture the value after the equal sign.

Now, I know what you’re thinking: isn’t that just like a shortened UTM parameter? It’s still kind of gross. Yeah, you are not wrong. Queries are still not the most attractive thing in the world. At GBDA, we’re working on a plugin that will allow 301 redirects from folders to query strings. So https://www.greatbigdigitalagency.com/event/comiccon would redirect to https://www.greatbigdigitalagency.com/?event=comiccon. This would give complete ability to make simple, beautiful urls that grab data and send the query info to Google Analytics as a custom dimension. Anyway, for now, lets just go through the concept. Pick your query code (we are using event) and then update your Google Analytics with new query variable. This is now a completely dynamic capturing value. Feel free to create new event and add them after the query for dynamic capturing. https://www.greatbigdigitalagency.com/?event=wedidit Now, we just wait and capture the data! You did it! I’m proud of you, kid. If you are looking for more interesting ways to grab dynamic data, check out our post: GTM DataLayer Sifter:Find the data you want easily.


About Great Big Digital

Achieve your website goals with customized data, intuitive UX, and intentional design.