I updated Google Tag Manager based JavaScript to use the klaviyo object rather than _learnq a week ago. I have noticed a significant drop in the volume of events tracked against profiles since doing so.
I have read both https://developers.klaviyo.com/en/docs/introduction_to_the_klaviyo_object and https://developers.klaviyo.com/en/docs/javascript_api and I think I have become a little confused. Is there any difference between calling klaviyo.track(eventName, eventData)
vs klaviyo.push(“track”, eventName, eventData)
?
For more detail, on our website, an email address is added as a body attribute within the page code when a visitor is logged in. Therefore, I want to call identify with this email address if they are not already cookied before tracking an event. Otherwise, I only need to track the event (because a previous email click through to the site or form submission will have cookied the user/browser..?).
The original code I had didn’t cater for this:
<script type="text/javascript">
var _learnq = window._learnq || [];
var item = {
"ProductName": "Pricing for Individuals",
"Categories": ["Individual Pricing","Engagement","View","B2C"],
"Action": "View",
"URL": {{Page URL}},
"page": {{Page Hostname}} + {{Page Path}},
"path": {{Page Path}}
};
_learnq.push(["track", "Viewed Product", item]);
</script>
And I have changed this to:
<script type="text/javascript">
function trackEvent(eventName, eventData) {
klaviyo.track(eventName, eventData);
}
var eventName = "Viewed Product";
var emailStored = {{CJS - PRIMARY - Email}};
var eventData = {
"ProductName": "Pricing for Individuals",
"Categories": ["Individual Pricing", "Engagement", "View", "B2C"],
"Action": "View",
"URL": {{Page URL}},
"page": {{Page Hostname}} + {{Page Path}},
"path": {{Page Path}}
};
function trackKlaviyoEvent() {
if (typeof klaviyo !== 'undefined') {
if (emailStored) {
klaviyo.identify({'email': emailStored}, function() {
trackEvent(eventName, eventData);
});
} else {
trackEvent(eventName, eventData);
}
}
}
// Track the event immediately
trackKlaviyoEvent();
</script>
What am I missing? I’m suspicious that the check for klaviyo being undefined might have something to do with this, but should I be calling klaviyo.track or klaviyo.push in the trackEvent function?
Thanks!
Ross