Flow: Parse UTM Parameters from URL

Updated 2020-01-09: Improved the Flows to get only the UTM parameters to parse. Templates updated also.

For those unaware, what are UTM parameters, they are additions to the URL that help marketing people segment a link in a specific campaign, medium, and content. You can see this usually when you open a link, in an email or click specific links in websites, but they always keep a format like this:

https://www.test.com?utm_medium=newsletter&utm_campaign=test-campaign&utm_content=promotion

Google Analytics, or whatever system you’re using, pick them to keep tabs on what’s working and what’s not. Google Analytics introduced UTM parameters for that specific purpose, and used widely in the industry, even in competing services.

Why extract them

Sometimes it’s handy to have that information elsewhere so, given a specific URL, we can quickly parse these values and get them individually and store them. For example, if you want to keep a list of all parameters used in campaigns and provide that list to the marketeers, then you need to get them individually. You can give them a Flow that they can feed a URL, and that information is automatically stored for them to reference in the future.

Flow to get the parameters

I’ve built two templates that you can find in my template section, one if you want to have the full flow and another if you’re going to have a Flow that performs these actions and then returns the values. The later can be quite useful if you want to re-use this logic elsewhere but requires you to have a premium account because we’re using the “Request” trigger.

There are three main steps for parsing the URL.

Get the individual elements

Let’s use the example above and split it using the & as our separator.

The formula is quite simple:

split(triggerBody()['text'],'&')

We’ll get something like this:

[
  "https://www.test.com?utm_medium=newsletter",
  "utm_campaign=test-campaign",
  "utm_content=promotion"
]

Get the values of the elements

Now that we have the items, we need to get each of them and split them again, this time by “=.”

The formula is the same:

split(items('Apply_to_each'),'=')

And we’ll get something like this:

First run:
[
  "https://www.test.com?utm_medium",
  "newsletter"
]

Second run:
[
  "utm_campaign",
  "test-campaign"
]

Third run:
[
  "utm_content",
  "promotion"
]

Setting the values to variables

The rest is trivial. Add a Switch and set them to the correct variable.

Notice that the default is “utm_medium.” It’s usually the first one, and we’ll get the URL also, so if we can get the other two, then we’re sure that the last one is this one.

If you’re using the Request trigger

The “Request” trigger allows you to centralize a lot of business logic into one Flow. There are only two extra things that you need to take into consideration that I’ll highlight now.

I’ll include, in the template, archive an example of a Flow that calls a service that is a “Get” instead of a “Post” so that you can see the differences.

Request Type – GET

The request type needs to be “Get,” since we’re trying to get something back from the service, in this case, the values parsed. To do this, just set, in the advanced settings, the “method.” Notice that we’re not expecting anything in the body of the request since the URL is in the “Header” portion of the request

Send Response

Now let’s prepare the information to send back to the caller of the service. To do this, do the following:

The calling Flow will get a helpful JSON formatted reply with the UTM parameters passed.

One more thing

I got an email from Sam (my thanks to Sam!) that had a perfect point. The templates work if you have URLs that contain UTM parameters only. If not we need to add an extra filter to filter them out.

Here’s the change:

The formula to filter is the following:

indexOf(item(), 'utm_')

We’ll filter out all items that don’t contain the string “utm_.” The templates were updated to consider this.

That’s it.

It’s quite simple, and it can give you a lot of flexibility to parse information internally.

There’s no need for you to build this yourself. I have a template that you can download and it in your instance. Just use the template archive, and you’ll find these and more.

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and be sure to check out other Flow-related articles here.

Photo by Nikita Sypko on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

4 thoughts on “Flow: Parse UTM Parameters from URL

  1. Just wanted to say thank you for answering this question for me on the forum. This is a great article. Loving Flow and what it can do.

    I’m finding some of the out of the box triggers and steps are super powerful. You can integrate with almost any system using them (not just ones with existing connectors in Flow).

    Mike

  2. Hello Manuel,

    Thank you for this wonderful article. I am following the steps you mentioned in this document but there is something I am struggling with. Basically, I have a field in my dynamics marketing contact form which has the url the contact came in from. I am saving this url because it contains the utm parameters. What I am trying to do is breakdown these utm parameters and store in individual fields on the contact record. I imported your template and it runs successfully. I want to know if I should create a new flow for this or I can use your template and how it will storing values in dynamics marketing.

    If you can provide a guide on this would be really helpful. Thank you so much.

Leave a Reply

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