Solved

Getting the user email address from a cookie

  • 17 May 2021
  • 7 replies
  • 2228 views

Badge +2

Hi,

This is my first Klaviyo integration.

I’m working on a custom integration and trying to extract an email address from the cookie in a user’s browser.  I need the email address so that I can then be sure I am updating the same profile with server side APi calls.

In this document

https://help.klaviyo.com/hc/en-us/articles/360034666712-About-Cookies-in-Klaviyo

it says that I can retrieve this with the following code:

 _learnq.identify().$email

This is returning a jQuery error saying that _learnq.identify is not a function.

What am I doing wrong?  Or is there another, better way for a server side API call to identify a user that has been cookied on a previous visit to the site (so the cooie is the only way to identify them)?

Cheers,

Crac

icon

Best answer by jallain 18 May 2021, 14:25

View original

7 replies

Userlevel 4
Badge +11

Hello @Caractacus Downes , have you already included Klaviyo’s onsite javascript? If you haven’t, take a look at this article which explains how you can: https://help.klaviyo.com/hc/en-us/articles/115005082927-Guide-to-Integrating-a-Standard-Ecommerce-Platform

 

_learnq should be defined as an object on your page and not just a javascript array, if it is just an array then it could be that you are trying to re-define it in another snippet but it is not picking up the global object.

You can also call the Identify API server-side if you would prefer to do that as well. Take a look at our API docs, specifically this section: https://apidocs.klaviyo.com/reference/track-identify#identify

Badge +2

Hi Jallain,

Thanks for responding.

I understand how the _learnq object works, and I’ve checked it has loaded and is an object before I call the identity() method, but it still come sback with the error.  If I call the method from the Chrome console it works, weirdly.

I’m aware of the server-side API Identify call, but that’s not going to help here I think.  The specific scenario I’m trying to handle is when a user, whi has previously registered on the site and so has the cookie installed on their browser, returns to the site.  The JavaScript tracking works with the existing cookie, but the logical place for me to add tracking events like adding items to a shopping basket is on the server, where I can do it in a single place, rather than in the browser, where I would have to implement it in all the places a user can add items to the basket.  To do that I need to let the server know who the user is, so I need to get the email address out of the cookie.

I’ll do some more work on it today and see if I can work out what’s going wrong, but any more suggestions gratefully received.

Cheers,

Crac

 

 

 

 

Userlevel 4
Badge +11

@Caractacus Downes are you defining the _learnq object in the snippet that is throwing the error? If so you can try a couple of things to see if it helps. If you have a line like this at the beginning of the snippet:

var _learnq = _learnq || []

you can change it to look like this:

var _learnq = window._learnq || []

This should help if the problem is a scoping issue.

It could also be a timing issue if the _learnq object just isn’t being defined properly before the code tries to access its identify method. If that is the case you can try delaying the snippet which calls identify with a setTimeout. Let me know if you need assistance with that.

Badge +2

Hi Jallain,

I think I’ve got it behaving now.  It did turn out to be a timing issue - I thought I had the relevant calls happening after the page was completely loaded, but adding a 1 second delay has fixed the problem.  Feels like a bit of an unpredictable fix, but most of the instances in which I’ll use it will happen long after page load so shouldn’t be a problem.

Thanks for your help.

Cheers,

Crac

 

 

Badge +4

@Caractacus Downes 

I know it’s been about 9 months since you posted this but are you comfortable sharing any custom code snippets? I’m trying to do the same from Shopify at the login page. Users that have previously signed up and have a login need to be tracked as well on our site. I tried to follow the custom API call tutorial but I got a little lost in the details.

Userlevel 7
Badge +58

Hi there @L Padi,

While waiting to hear back from what @Caractacus Downes found for a solution, I would recommend looking into our API docs to see if you can find your solution there first from reading. 

What detail are you getting stuck on exactly? Is it returning an error? Also, I would double check with Shopify support as well to see if the issue could be on their end.

 

Thanks,

Alex

Badge +2

FYI I think it might no longer be supported to grab an email address from the Klaviyo cookie, though I’d love to be proven wrong on this…

 

Reply