Solved

How can I pass UTM parameters when subscribing a user to a list via the API?

  • 9 November 2023
  • 8 replies
  • 178 views

A site I’m working on has a mailing list subscription form. On submission, the user is subscribed to a Klaviyo list via the API.

Previously, there was an embedded form, and UTM tracking information from query parameters was automatically included, and showed up against the user so my client could see where they came from.

Now, however, we’ve built a custom solution which does not use the Klavyio-provided JS, and instead communicates with the Klaviyo API directly. There is currently nothing passing through any UTM query parameters to Klaviyo. I need to restore this feature.

At the moment I’m using a 3rd-party integration with Klaviyo, and I can see from reading its source code that it is using the deprecated v2 version of the Klaviyo API. I’ve opened a feature request for them to update this, but that is not the primary concern right now.

I am not averse to writing and submitting a patch to that integration which adds the tracking information submission. I don’t much care whether I also upgrade it to the new API at the same time or not; whichever is easiest. I can worry about the deprecation later if it’s simplest to leave it at v2 for now.

So: is it possible to send the UTM tracking data with the v2 subscription API call? I can’t see anything about it in the relevant v2 endpoint documentation, so if it is possible, how do I do it? If I need to make a separate call after or before subscription, what would those look like? If it’s not possible with v2, how is it done with the current API?

icon

Best answer by KeviSunshine 9 November 2023, 22:17

View original

8 replies

Userlevel 4
Badge +7

Hi @bjn,

I don’t quite understand–how do you want the UTM parameters to show up in Klaviyo? Do you want to add them as profile properties? Or are you attempting to fill out the “How they found you” sections on a profile? (See screenshot)

 

Let us know.

Cheers,

Kevin.

 

P.S. Not sure why the image is so large and I can't figure out how to resize it so :shrug:

I don’t quite understand–how do you want the UTM parameters to show up in Klaviyo? Do you want to add them as profile properties? Or are you attempting to fill out the “How they found you” sections on a profile?

Sorry for the confusion; I haven’t actually seen how things look in Klaviyo, I’m acting based only on what the client is reporting. The “How they found you” section has to be right.

Userlevel 4
Badge +7

Hi @bjn,

Understood, understood.

I don’t think it’s possible to set the “How they found you” variables via the API, but I don’t know that for sure! We’d have to ask a more experienced Klaviyo person like @Brian Turcotte … Brian–let us know if the “How they found you” section can be updated from an API.

But @bjn, for your use case, I would just keep the default Klaviyo script on your client’s site. You don’t need to use it for things like subscribing profiles, but keep it there to track UTMs like this (which it does by default). This is what we do on our custom applications (we use a custom API layer that we wrote, but still include the script for UTMs)

And for clarity, this is the script we include: `https://static.klaviyo.com/onsite/js/klaviyo.js?{query params for identification removed}`

I see, so it’s not as simple as those fields being filled by the subscription alone; other mechanisms are at play.

Thank you for explaining and for the suggestion. If that’s the only option I’ll go with it, but I’ll wait to hear back from Brian since I’d prefer an option which doesn’t need the Klavio JS to load, out of performance concerns. (In terms of payload alone, I tested loading that script in an otherwise-empty HTML file, and it downloads 48K which decompresses to 106K of JS. That’s a lot just for this one feature.)

Userlevel 7
Badge +36

Hi @bjn!


@KeviSunshine is correct - at this time, it’s not possible to upload the “How they found you” field via API. However, I have spoken to our API engineers about this and it’s something they’re considering adding support for in the future. That said, I would agree with @KeviSunshine’s workaround for your use case here.

 

Best,

Brian

Alright, thank you very much.

Hold on, is this really going to “just work”?

I was about to get word to the client that they need to just add the Klaviyo tracking script in their GTM setup and then everything should function as they wanted, but I’m not sure it’s going to be quite as simple.

The tracking script presumably fingerprints the user and collects the UTM query params and such, and so associates the user with this current IP or session or whatever.

Then when that same user signs up to a mailing list it would know it was the same person, and so we know where they come from.

But the mechanism for the signup in this case is via a back-end process. The user submits a form, it goes to the server and is processed, and then the API call is made to Klaviyo from there. As far as I can tell from the source code the user’s IP address isn’t sent. The code is at https://github.com/verbb/formie/blob/craft-4/src/integrations/emailmarketing/Klaviyo.php#L124 -- it’s making two calls, one to `identify`, another to `v2/list/[id]/subscribe`. I can’t see anything there sending an IP address or the contents of any tracking cookie or anything like that.

Is there some magic I’m not seeing and it’ll still somehow be associated with the UTM params which were on the client-side page?

(Trying again to add a followup here -- I tried once before and it said “awaiting moderation” but then never appeared nor did I get any kind of notification. I didn’t realize marking the answer would irrevocably close the thread, and I’ve had no response from staff either when I asked about reopening it.)

The answer above was not actually a complete solution in this case. The tracking script running on the client did not get the mailing list signup correlated. Looking back it should have been clear to me that wouldn’t work, since the mailing list signup was happening as a back-end process, where of course none of the tracking script’s findings and mechanisms are present.

The complete solution was to not only run the tracking script, but also to make an “identify” call with the user’s email address as/after the signup:

if (window.klaviyo) {
const emailInput = [...form.elements]
.find((el) => el.tagName === "INPUT" && el.type === "email");
if (emailInput != null
&& !emailInput.validity.valueMissing
&& emailInput.validity.valid) {
window.klaviyo.identify({ $email: emailInput.value });
}
}

 

Reply