Solved

A better way to load Klaviyo object?

  • 11 October 2023
  • 2 replies
  • 387 views

Badge

Hi everyone,

I am working on a React base Custom Storefront for my Shopify shop using Klaviyo. In order to access Klaviyo’s object to push or identify a user, I am needing to load the Klaviyo object per page. According to the docs, it looks like I have to use this code snippet on each page that I want to use the object on.
 


This code snippet is not the cleanest looking and has a lot of abstraction on first glance.

I wanted to ask the community if there is a better way to implement this with React without having to load this snippet into each page or at the very lease, make it into a reusable component.

The documentation I am referencing in the screenshot is here:
https://developers.klaviyo.com/en/docs/introduction_to_the_klaviyo_object#how-to-load-the-klaviyo-object

Thank you guys for your help.

icon

Best answer by KeviSunshine 11 October 2023, 22:39

View original

2 replies

Userlevel 4
Badge +7

Hi @SetoSeng,

I build and interact with React Apps that integrate with Klaviyo too.

One of the big questions here is whether you’re dealing with an SPA or a server-based app.

Regardless, you could certainly use that snippet, and I’d recommend loading it before you load your app. I have my third party scripts (like user analytics platforms) loaded before loading the app itself.

Here’s an example of my code for an SPA showing how I initialize (slightly edited).

init().then(({ store }) => {
const MOUNT_NODE = document.getElementById(APP_ELEMENT_ID);

ReactDOM.render(
<Provider store={store}>
<AppRouter />
</Provider>,
MOUNT_NODE
);

afterRender(store);
});

The `init` function loads all my third party data (and yes, I literally load a JS file as abstract as the snippet you posted for my third party integrations).

However, just so you know, I don’t actually use that Klaviyo object. I wrote a pretty simple Klaviyo API layer, and I call my own functions when I want to track things to Klaviyo. Please note that this API layer is only for the “public” endpoints from Klaviyo. It does not use the API Secret, just the API Public key.

Here’s an example…

export const track = (event, customerProps, eventProps) => {
const data = {
token: /* Your Public API key */,
event, // Event name
customer_properties: customerProps, // Update any customer properties here
properties: eventProps // Event properties go here.
};
return axios.post(TRACK_ENDPOINT, new URLSearchParams({ data: JSON.stringify(data) }));
};

I use this API layer across multiple projects (including some server side calls, some Next.js apps, and SPAs).

So the takeaway is there are a lot of ways of working with Klaviyo in React. Let me know if you have any more questions.

 

Cheers,

Kevin.

Badge

Hi @SetoSeng,

I build and interact with React Apps that integrate with Klaviyo too.

One of the big questions here is whether you’re dealing with an SPA or a server-based app.

Regardless, you could certainly use that snippet, and I’d recommend loading it before you load your app. I have my third party scripts (like user analytics platforms) loaded before loading the app itself.

Here’s an example of my code for an SPA showing how I initialize (slightly edited).

init().then(({ store }) => {
const MOUNT_NODE = document.getElementById(APP_ELEMENT_ID);

ReactDOM.render(
<Provider store={store}>
<AppRouter />
</Provider>,
MOUNT_NODE
);

afterRender(store);
});

The `init` function loads all my third party data (and yes, I literally load a JS file as abstract as the snippet you posted for my third party integrations).

However, just so you know, I don’t actually use that Klaviyo object. I wrote a pretty simple Klaviyo API layer, and I call my own functions when I want to track things to Klaviyo. Please note that this API layer is only for the “public” endpoints from Klaviyo. It does not use the API Secret, just the API Public key.

Here’s an example…

export const track = (event, customerProps, eventProps) => {
const data = {
token: /* Your Public API key */,
event, // Event name
customer_properties: customerProps, // Update any customer properties here
properties: eventProps // Event properties go here.
};
return axios.post(TRACK_ENDPOINT, new URLSearchParams({ data: JSON.stringify(data) }));
};

I use this API layer across multiple projects (including some server side calls, some Next.js apps, and SPAs).

So the takeaway is there are a lot of ways of working with Klaviyo in React. Let me know if you have any more questions.

 

Cheers,

Kevin.

Thank you so much for your response. This is a very eye opening solution prompting me to look further into using the API layer instead since my app is server-based running on Shopify Hydrogen/Remix. 


I am still very new to developing integrations with Klaviyo so I was not aware we could make requests to their end points. Most of the JavaScript documentations was to load the Klaviyo object and used it’s methods. 

 

Thanks again for your insight! 

Reply