Skip to main content
Solved

klaviyo.isIdentified() Promise "pending"

  • August 22, 2024
  • 6 replies
  • 131 views

Forum|alt.badge.img+1

I’m implementing the new klaviyo object and when running the ‘klaviyo.isIdentified’ method the Promise is always “pending”. I’m trying to get the script to identify based on the ‘__kla_id’ cookie value.

Anyone else run into a never changing ‘pending’ return?

Best answer by KeviSunshine

Weird! I don’t use the Klaviyo object, but I figured the Klaviyo forums do, so I opened up the console and was messing with some similar logic as you… See the screenshot...

 

if (klaviyo?.identify) {
    klaviyo.identify({}).then(() => klaviyo.isIdentified().then(res => console.log(`Klaviyo is identified: ${res}`)));
} else {
    console.log('klaviyo object not yet initiated')
}

 

I am not sure what else to try, but perhaps you can see if your logic is functioning differently on klaviyo’s site than on your personal site? That could give you a baseline to test from.

 

Sorry about that!

 

Best,

Kevin.

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

6 replies

KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 159 replies
  • August 22, 2024

Hi @dualsided,

I haven’t used the new Klaviyo object, but I’ve experienced similar issues with JS promises.

Can you share your code snippet that is calling the `isIdentified` function, and tries to get the ID out of it?

 

Cheers,

Kevin.


Forum|alt.badge.img+1
  • Author
  • Contributor I
  • 3 replies
  • August 22, 2024

Sure. The ‘Klaviyo: Identified’ never fires and ‘Klaviyo: isIdentified’ always returns a pending promise.

The browser has a ‘__kla_id’ set.

 

I’m loading this in a Shopify Custom Web Pixel so there is not ability for “HTML”. The ‘klaviyo’ object successfully exists.
 

const script = document.createElement('script');
script.setAttribute('src', 'https://static.klaviyo.com/onsite/js/klaviyo.js?company_id='+accountId);
document.head.appendChild(script);

!function(){if(!window.klaviyo){window._klOnsite=window._klOnsite||[];try{window.klaviyo=new Proxy({},{get:function(n,i){return"push"===i?function(){var n;(n=window._klOnsite).push.apply(n,arguments)}:function(){for(var n=arguments.length,o=new Array(n),w=0;w<n;w++)o[w]=arguments[w];var t="function"==typeof o[o.length-1]?o.pop():void 0,e=new Promise((function(n){window._klOnsite.push([i].concat(o,[function(i){t&&t(i),n(i)}]))}));return e}}})}catch(n){window.klaviyo=window.klaviyo||[],window.klaviyo.push=function(){var n;(n=window._klOnsite).push.apply(n,arguments)}}}}();

klaviyo.identify({}, (result) => console.log('-- Klaviyo: Identified'));
console.log('-- Klaviyo: isIdentified ', klaviyo.isIdentified() );

 


KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 159 replies
  • August 22, 2024

I am not 100% sure what’s happening here, but try these two things…

  1. Put the `isIdentified()` call in the callback for the `identify` function: `klaviyo.identify({}, () => console.log(klaviyo.isIdentified()));`
  2. Try using the callback parameter that isIdentified accepts (according to the docs): `klaviyo.isIdentified(result => console.log(result));`

I believe you’re calling identify, which returns a promise, and then trying to call isIdentified right away before the promise is resolved. And the browser’s console doesn’t show this feedback well.

 

Best,

Kevin.


Forum|alt.badge.img+1
  • Author
  • Contributor I
  • 3 replies
  • August 22, 2024

Still nothing being output. I even tried extending the Promise and no console logs. 
 

klaviyo.identify({}).then(
  () => console.log('-- Klaviyo: ', 'IDENTITY SUCCESS'), 
  () => console.log('-- Klaviyo: ', 'IDENTITY FAIL') 
).catch((error) => console.log('-- Klaviyo: ', 'IDENTITY ERROR: '+error) );

I don’t have the users email but I do have the `__kla_id` value. I don’t see in the documentation if there is a way I can pass that value to the klaviyo object incase its not consuming it automatically.

I also added console logs before and after the code above just to make sure it was running and those output just fine. 


KeviSunshine
Expert Problem Solver III
Forum|alt.badge.img+8
  • Expert Problem Solver III
  • 159 replies
  • Answer
  • August 23, 2024

Weird! I don’t use the Klaviyo object, but I figured the Klaviyo forums do, so I opened up the console and was messing with some similar logic as you… See the screenshot...

 

if (klaviyo?.identify) {
    klaviyo.identify({}).then(() => klaviyo.isIdentified().then(res => console.log(`Klaviyo is identified: ${res}`)));
} else {
    console.log('klaviyo object not yet initiated')
}

 

I am not sure what else to try, but perhaps you can see if your logic is functioning differently on klaviyo’s site than on your personal site? That could give you a baseline to test from.

 

Sorry about that!

 

Best,

Kevin.


Forum|alt.badge.img+1
  • Author
  • Contributor I
  • 3 replies
  • August 23, 2024

So interesting test…
This loads correct on initial session start. If I go incognito the consoles fire on the first page loaded. On any subsequent page the consoles do NOT fire. This is an issue because the klaviyo tracking event never tracks unless the identify is successful.