I asked Chat GPT to create a Klaviyo Event that every time a user logs in, Send the Event to Klaviyo. Doesn’t seem to work correctly. Perhaps someone could take a look at the code and see what is wrong:
<!-- Klaviyo User Login API Event -->
{% if customer %}
<script>
// Set your Klaviyo API key and event name
const klaviyoApiKey = "KLAVIYO_API_KEY_HERE";
const eventName = "KLAV USER LOGIN";
// Get the Shopify customer ID and email
const customerId = "{{ customer.id }}";
const customerEmail = "{{ customer.email }}";
// Send the event to Klaviyo
const sendKlaviyoEvent = () => {
// Set the data for the event
const eventData = {
"token": klaviyoApiKey,
"event": eventName,
"customer_properties": {
"$email": customerEmail,
"shopify_customer_id": customerId
}
};
// Send the event data to Klaviyo
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://a.klaviyo.com/api/track");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(eventData));
};
// Listen for the customer login event
document.addEventListener("login", () => {
// Call the function to send the event to Klaviyo
sendKlaviyoEvent();
});
</script>
{% endif %}
<!-- Klaviyo User Login API Event -->