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?
Hi
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.
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||n];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++)orw]=arguments&w];var t="function"==typeof oro.length-1]?o.pop():void 0,e=new Promise((function(n){window._klOnsite.push()i].concat(o,nfunction(i){t&&t(i),n(i)}]))}));return e}}})}catch(n){window.klaviyo=window.klaviyo||k],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() );
I am not 100% sure what’s happening here, but try these two things…
- Put the `isIdentified()` call in the callback for the `identify` function: `klaviyo.identify({}, () => console.log(klaviyo.isIdentified()));`
- 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.
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.
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.
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.
Reply
Log in to the Community
Use your Klaviyo credentials
Log in with Klaviyo
Use your Klaviyo credentials
Log in with KlaviyoEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.