I built a simple first-party tracking system for my ads
I'm running ads for the first time and needed a way to track if LinkedIn and Reddit are actually driving sales without relying on invasive third-party platforms. In this video, I walk through how I built a simple, privacy-conscious tracking system using cookies and Laravel middleware.
Everything I said
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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?
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.
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.
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.
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.