---
title: I built a simple first-party tracking system for my ads
url: "https://aaronfrancis.com/videos/i-built-a-simple-first-party-tracking-system-for-my-ads"
path: "/videos/i-built-a-simple-first-party-tracking-system-for-my-ads"
type: video
author: Aaron Francis
published: 2025-07-18
topic: "Career & craft"
keywords: [first-party tracking, utm parameters, privacy-first tracking, laravel middleware, ad tracking, cookies, json column, anonymous tracking, ad roi, linkedin ads, reddit ads, user attribution]
youtube_url: "https://www.youtube.com/watch?v=g8pqMj5zxQo"
site_index: "/llms.txt"
keyword_index: "/llms/keywords.txt"
---

# I built a simple first-party tracking system for my ads

## Chapters

- Running ads for our courses
- Why I need first-party tracking
- Using UTM parameters to track sources
- Strategy: store UTM params on login
- Using cookies for anonymous visitors
- Logging in triggers cookie check
- Writing UTM data to the user record
- Building it as Laravel middleware
- Capturing UTM params from requests
- Fallback to cookies if no params
- Logic for logged-in vs anonymous users
- Storing UTM data in a cookie for one year
- Reading and decoding the cookie
- Storing UTM data in JSON column
- Deleting the cookie after writing to user
- Final logic review of the middleware
- Benefits of simple, privacy-first tracking
- Tracking ROI from LinkedIn and Reddit ads
- Final thoughts and ad help request

## Transcript

**Aaron Francis:** All right, I'm doing something I've never done before, which is buying ads. I'm buying ads on LinkedIn, and I'm buying ads on Reddit, and I need to track, I don't know, if they're actually working. What do, what do I know about ads? If you're not familiar, uh, me and my business partner h- uh, run an education business. We have courses on, uh, SQLite, courses on Postgres, uh, courses on how to make videos just like this video that you're watching right here.

**Aaron Francis:** And if you want any discounts, you can use the code YouTube, which is down below. But here's the thing. I gotta figure out if these ads are working, right? I, I don't, I don't know anything about ads, so I gotta figure out some tracking. So here's what I'm gonna do. Um, when, uh, somebody comes from... Let's say they go to Mastering, if I could spell it, masteringpostgres.com.

**Aaron Francis:** Maybe that's our problem. Maybe it's not spellable. Uh, utm_source=linkedin. Now, there's gonna be a bunch of different parameters on there, but I need to track that. I need to store that, um, so that I know later if they buy, that LinkedIn is converting. I don't wanna set up Google AdWords, Google whatever, you know, GA4 or whatever.

**Aaron Francis:** So we're gonna roll it ourselves because it's not that difficult. So the first thing we're gonna do is we're gonna say when they enter, when they enter the website, let's do this. Let's say, um, are, are they logged in? 'Cause if they're logged in, if they're logged in, we can do something very simple.

**Aaron Francis:** We can just say write the, uh, params to the user record. That's easy. So whoop, that's gonna be the easy version. Now, the harder version is are they not... We gotta put a yes there. Yes, they're logged in. If they're not logged in, what are we gonna do? We're gonna add the params to a cookie.

**Aaron Francis:** Now, this is nice because this is a first-party cookie. Well, this is a, we're not using some fancy tracking platform that's gonna track you all around the internet. We're just writing a freaking cookie that we own. So if they're logged in, add the params to a cookie. And then we wait, right? Then we wait, and then hopefully they come back at some point, and they enter like this.

**Aaron Francis:** So they'll enter the website, um, like this with no params, and they'll say, "You know what? That guy seems really smart. That guy seems really smart. I should go buy that guy's course. What was his name? Oh yeah, it was at masteringpostgres.com." So they come to masteringpostgres.com. They log in because they're gonna buy.

**Aaron Francis:** They log in, and then what we do is we check is there a cookie? Is there a cookie? If there is a cookie, which is gonna be our only case, 'cause if there's not a cookie, we don't do anything. But if there is a cookie, we are going to write the params to the user record. So what we've done is we've successfully, uh, tracked them across multiple visits.

**Aaron Francis:** First party tracking, sharing with nobody. Uh, when they come in with a UTM source, if they're not logged in, we're gonna add the params to a cookie for one year. That's a long time. Is that too long for attribution? I don't know. You tell me. Literally, you tell me. I don't know. Um, and then whenever they come back later, and they log in, we're gonna check is there a cookie, and if there is, we're gonna write the params to a user record, which you'll see looks very similar to that because it is

**Aaron Francis:** in fact that exact thing. We can simplify all of this into a single middleware, which is what we're gonna do. In my case, we're gonna be using Laravel. That's basically just an implementation detail. You can use this with any framework that offers middlewares that run on every request. So every incoming request, we're just gonna check for a few things.

**Aaron Francis:** Here is our stub. We have a UTM tracking middleware that has nothing in it, and I think we've already added it to, if we could find bootstrap app, I think we have already added it to UTM. There you go. So we've got a couple of different middlewares, and then we've got our UTM tracking middleware, and that's where we're gonna live right here.

**Aaron Francis:** First thing we're gonna do is we are going to see request, uh, only. We're gonna grab all of these keys. So all of these are UTM, and then sometimes we'll throw a ref on something. That's pretty standard as well. And then we'll say that that is the data. Now, if there is no data, so if the data is empty, not just ref, if the data is empty, we should also check the cookie.

**Aaron Francis:** So we're gonna check the cookie, but we don't know what the cookie is yet 'cause we haven't set it. So if the data is empty, check the cookie, and we'll set data equal to cookie. Um, and we'll pull the data out of the cookie. Now, um, what do we need to do next? We need to do if auth check. So if they are logged in, um, and the data is not empty, so the data is not empty, then write to the user

**Aaron Francis:** record. Uh, else if, what do we wanna do? So if they're not logged in, let's say, um, that the data is not empty. So if they are logged in, and there is some data, we're gonna write it to the user record. Otherwise, if the data is not empty, we need to add it to the cookie.

**Aaron Francis:** I think that is all we have to do. I think that satisfies our diagram over here. Uh, let's just keep going and see if we can figure this out. So let's first add it to the cookie. We're gonna say cookie queue. So we're gonna queue this into the response, and we'll say, yeah, that is close- That's not quite right.

**Aaron Francis:** We'll say UTM parameters, and we're just gonna JSON encode the data. That part is right. And then I said 60 minutes times 24 hours times 365. We're gonna just queue it up for a year. Now, now that we know the name of the cookie, we can come up here and say, um, let's try this.

**Aaron Francis:** Let's do cookie get UTM parameters, and what is this get and default? Mm. Yep. So we'll do, uh, we'll just say cookie equals, and then... And we don't need a false. We can say if, if cookie, then data, which is this guy up here, so we're just reusing that variable.

**Aaron Francis:** Data equals JSON decode cookie, and we do want an associative. I think that is correct. So now we have set, we've checked the cookie, which is that guy, and we've set the cookie to data. Okay? So then this should fall through, and if they're logged in, and there is some data, we need to write it to the user record, which we can just say auth user, and then we can update.

**Aaron Francis:** And I have a, uh, UTM parameters. Let's just check the user record, or the user model really quickly. User model UTM parameters, and the UTM parameters is cast as an array, so we don't have to JSON encode it. So what we're doing is we're just storing all of the UTM parameters in a single JSON column because I don't know what else to do with it yet.

**Aaron Francis:** We don't have any reporting. Eh. I just need to store the data. We'll store the data for now, and we'll figure out the rest later. The last thing we need to do is once we've committed the cookie, or rather the data in general to the user record, we need to destroy that cookie. So what we're going to do is we're just going to queue up a cookie forget of UTM parameters.

**Aaron Francis:** And I think that is it. This could be, I mean, this could be made a little bit nicer. This could be data equals cookie. Um, yeah, that feels a little bit nicer to me. And then maybe add a space there. Uh, yeah, that feels good. So let's, let's follow this all the way through.

**Aaron Francis:** You could, um, this is, there's also a request cookie. You could do that. Doesn't matter. It's the exact same thing. In fact, if you go click in request cookie, that's exactly what it is. So don't super care about that. So the first thing we're gonna do, on every single request, we're gonna pull off the data. If there is no data, we're gonna see, hey, is there a cookie?

**Aaron Francis:** Um, if there is, we're gonna decode it and set it to data. Then we're gonna check to see if they're logged in, and there is some data. And if so, we're gonna say, "Hey, we got it. You can forget it. We got it now." We're gonna write it to the user record into a JSON column. Um, otherwise, if they, if the check, auth check fails, but the data is full, we're just gonna queue it up for a year.

**Aaron Francis:** In fact, I think this is infinite because I think we're gonna re-queue it on every request, which, eh, I don't know. Doesn't really matter to me. And then we're just gonna keep the, uh, we're gonna keep the request chain going. So there you go. There is your, uh, there's your poor man's tracking. Let's just say there's your privacy conscious first party tracking system.

**Aaron Francis:** Eh, it's also a poor man's tracking system, but this is gonna work great, right? So now in all of our emails and all of our tweets and all of our ads, hopefully ads convert. If you know anything about ads, please tell me. I'm trying to make it work. Um, but now we can track where these people are coming from, hopefully compute an ROI, spend a bunch of money, and make a bunch more money.

**Aaron Francis:** That's the dream. Uh, let me know what you think. I think this is a super simple way to do it. If a little bit naive, it still is going to work. I hope you enjoyed this. Until the next time. See ya.

---

## Related on aaronfrancis.com

- [Big strong kids use their hands for helping](/newsletter/big-strong-kids-use-their-hands-for-helping) *(Newsletter · 2025-07-25 · shared: first-party tracking, laravel middleware, linkedin ads)*
- [About](/about) *(Page · 2026-11-25 · shared: mastering postgres, high performance sqlite)*
- [Year in review: 2024](/writing/year-in-review-2024) *(Article · 2024-12-31 · shared: high performance sqlite, mastering postgres)*
- [Aaron Francis: Laravel Solo, Courses, Screen casting](/podcast/aaron-francis-laravel-solo-courses-screen-casting-e1626b9b) *(Podcast · 2024-12-20 · shared: high performance sqlite, mastering postgres)*
- [The Power of Putting Yourself Out There w/ Aaron Francis](/podcast/the-power-of-putting-yourself-out-there-w-aaron-francis-d8187592) *(Podcast · 2025-03-31 · shared: mastering postgres)*

## Relevant Resources from Aaron Francis

- **[Solo](https://soloterm.com)** (overview on site: [/software](/software)): All-in-one agentic development platform and terminal workspace built by Aaron Francis for orchestrating processes and AI coding agents.
- **[MySQL for Developers](https://databaseschool.com)** (overview on site: [/courses](/courses)): Full video course covering MySQL indexing, deferred joins, pagination, query plans, and schema design for application developers.

## Search aaronfrancis.com (2-Hop Static Keyword Index)

Pick the alphabetical shard matching any topic to find exact pages across all articles, newsletters, videos, talks, and podcast transcripts (every shard is < 40 KB):
[A-B](/llms/keywords/a-b.txt) · [C](/llms/keywords/c.txt) · [D-E](/llms/keywords/d-e.txt) · [F-G](/llms/keywords/f-g.txt) · [H-J](/llms/keywords/h-j.txt) · [K-L](/llms/keywords/k-l.txt) · [M-N](/llms/keywords/m-n.txt) · [O-P](/llms/keywords/o-p.txt) · [Q-R](/llms/keywords/q-r.txt) · [S](/llms/keywords/s.txt) · [T-Z](/llms/keywords/t-z.txt) · [Directory](/llms/keywords.txt) · [Site Map](/llms.txt)

Shards for this page's topics: `first-party tracking` ([F-G](/llms/keywords/f-g.txt)), `utm parameters` ([T-Z](/llms/keywords/t-z.txt)), `privacy-first tracking` ([O-P](/llms/keywords/o-p.txt)), `laravel middleware` ([K-L](/llms/keywords/k-l.txt)).

## About Aaron Francis (Current Context)

Aaron Francis is VP of Marketing and Community at [Laravel](https://laravel.com), founder of [Try Hard Studios](https://tryhardstudios.com), creator of [Solo](https://soloterm.com), co-host of [Mostly Technical](/podcast), and author of [Mastering Postgres](/courses), [High Performance SQLite](/courses), [MySQL for Developers](/courses), and [Screencasting.com](/courses). Full bio: [/about](/about) · Current focus: [/now](/now).
