Skip to main content
Solved

Custom Cart Integration and Klaviyo.Identify. When and Where?


Forum|alt.badge.img+2

I’m integrating Klaviyo into a custom shopping cart solution.

Currently I’m running the klaviyo.identify({ "$email": "some@email.com"}); only upon login or registration (this is when I get the user email). I assume that this is enough and once cookied Klaviyo will continue to track the user without the need of additional identify calls. But is that the reality?

Should I run identify on every page?

In the documentation I find this example klaviyo.identify({}); what is that good for if it send nothing to be identified upon. This confuses me even more.

Please explain when and where should I call klaviyo.identify. Thank you!

 

P.S.

I feel like I’m asking a stupid question but the documentation did not really help me. Google did not either. I even resorted to asking chatgpt but this thing is even more confused than me.

Best answer by Brian Turcotte

Hi @rollbg!

 

May I ask what is intended by adding an argument to klaviyo.isIdentified? This is typically only meant to determine if a user is cookied, and does not accept arguments. The previous example I gave was only intended to explain that a cookie will persist throughout the user’s entire session.

 

It may be useful to simplify this process by first checking if the user is logged in, and if they are, you only need to use this code to identify them:

klaviyo.identify({ '$email': $('#lnkProfile').data('email') })

 

I hope this helps to clarify!

- Brian

View original
Did this topic or the replies in the thread help you find an answer to your question?

5 replies

Brian Turcotte
Forum|alt.badge.img+37

Hi @rollbg!

 

This is definitely not a stupid question! Once a user is cookied, the cookie persists throughout their session and will remain whenever that user accesses the site (so long as it’s via the same device and browser) - this guide explains it more.

 

For testing, you can use the Chrome DevTools console to see if you are cookied or not. Screenshot 1 is a function you can run in the console for a NON-cookied user. Screenshot 2 shows the function output for a cookied user:
 

 

Essentially, you can use the javascript function klaviyo.isIdentified() in the console to see if you are cookied or not during testing. You can run the above function as you navigate your site to prove that the cookie will persist as you navigate to different product pages, etc.

 

I hope this helps, and thanks for using the Community!

- Brian


Forum|alt.badge.img+2
  • Author
  • Contributor I
  • 1 reply
  • March 31, 2023

Thank you very much for your answer. Based on your post I came up with the following solution:
 

if ($('#lnkProfile').data('email')) { //Checks if user is logged in
	console.log('User is logged in. Check if identified.');
	klaviyo.isIdentified(klaviyoIdentify);                            
};
function klaviyoIdentify(result) {
	if (!result) {
		console.log('User is not identified. Identify now.');
		klaviyo.identify({ '$email': $('#lnkProfile').data('email') }).then(() => console.log('Identify has been completed'));                                
	}               
};

The script is placed immediately after the Klaviyo.js snippet and the Klaviyo object definition.
My idea is to check if the user is logged in and then check if “isIdentified”. If not I call klaviyo.identify with the corresponding email.

Unfortunately it does not work as expected. When I load a page with logged in user I get this in the console:

As you can see promise is never returned. And I don’t see a call to Klaviyo’s servers in the network tab of the DevTools. Looks like the identify call fails silently.
But if I then run my function “klaviyoIdentify(false)” manually in the console it works:

My initial thought was that Klaviyo.js was not loaded properly yet when I call klaviyo.identify, but if that was the case I should get an error like “klaviyo is not defined” or something. Furthermore obviously klaviyo.isIdentified works so the library should be properly loaded right? I don’t get it.


Brian Turcotte
Forum|alt.badge.img+37
  • Klaviyo Alum
  • 1393 replies
  • Answer
  • April 6, 2023

Hi @rollbg!

 

May I ask what is intended by adding an argument to klaviyo.isIdentified? This is typically only meant to determine if a user is cookied, and does not accept arguments. The previous example I gave was only intended to explain that a cookie will persist throughout the user’s entire session.

 

It may be useful to simplify this process by first checking if the user is logged in, and if they are, you only need to use this code to identify them:

klaviyo.identify({ '$email': $('#lnkProfile').data('email') })

 

I hope this helps to clarify!

- Brian


Forum|alt.badge.img+2
  • Contributor I
  • 2 replies
  • April 18, 2023
Brian Turcotte wrote:

Hi @rollbg!

 

May I ask what is intended by adding an argument to klaviyo.isIdentified? This is typically only meant to determine if a user is cookied, and does not accept arguments.

 

According to this doc the isIdentified method supports a callback parameter. So once the isIdentified call resolves, it runs the user code to identify the session if it is not already identified. The results from @rollbg showed that the callback was started, but the identify call inside it doesn’t identify the session.


Brian Turcotte
Forum|alt.badge.img+37

Hi @TTook

 

Thanks for pointing that out! I’ll check on that with Engineering and update the thread as soon as possible.

 

Best,

Brian