utm_ params

Laravel — Track Google Analytics utm_* params for better marketing ROI — Simple Trick

Mo Kawsara
2 min readDec 22, 2021

--

TL;DR:

  1. Add a middleware to store all utm params in the session.
  2. Display these details, along others, as hidden inputs in CTAs.

No kidding, nothing fancy here, but it’s super effective and handy.

Jump to “The Fun Part” title below or be my guest and read the following boring into, you are appreciated no matter what 🧡.

Silly & Boring Intro 🙄

I am not here to tell you all about the importance of UTM query params, I am here to share an experience about getting advantage of these extreme beneficial params and how to make your marketing department happy now in Christmas 😌🎄

So if you want to track referrals from social media platforms, or perhaps a blog post somewhere, or maybe a link in an Email or in your client’s footer website, you will need the following:

  • Source (utm_source: Where)
  • Medium (utm_medium: How)
  • Campaign (utm_campaign: Why)
  • Content (utm_content: Details, read the following quote from Google answers)

Used to differentiate similar content, or links within the same ad. For example, if you have two call-to-action links within the same email message, you can use utm_content and set different values for each so you can tell which version is more effective.

  • Term (utm_term: Used Keyword, paid or non-paid keywords)

🧐 So why as a developer am I writing about this?

Because we will be implementing this in Laravel in order to keep track of these parameters and because it will be a way where we can embed them in our CTAs and lead forms. Besides, we will be giving one more reason for marketing people to feel nothing but respect to us 🤪

The Fun Part 💀 Coding (AKA Copy & Paste 😋)

In 2 simple Steps:

  1. Add a middleware to store all utm params in session.
  2. Display these details, along others, as hidden inputs in CTAs.

Step1:
Middleware `StoreMarketingQueryParamsMiddleware` used in Kernel.php

Step2:
Usage within a blade file:

Conclusion

Even though that you might think that this simple trick is really trivial, I just want to remind you that your end-user usually goes to your website and surfs around a couple of pages within it before filling out a lead form, so storing such details and overwriting it with new ones each time they get to your website is really helpful for marketing ROIs, especially with Google Analytics.

Thank you for reading, dear geek.

--

--