Javascript Events - Integration with MailChimp

Hi there!

I am doing a full integration with MailChimps API. This includes creating a ‘shop’ on the MC platform, passing all the product details into the shop.

After purchases managed by Snipcart, I use the " cart.confirmed" event to pass the purchased product data to MailChimp along with campaign ID’s etc. This is so we can track the conversion rates of campaigns and also enable automatic recommendations of products to buyers.

Its all working fine, except there is one thing I can’t seem to do. MailChimp can track abandoned carts but to do so it needs the cart contents and buyer email address before purchase.

If I use “cart.created” event I cannot access the buyer email (as its often not added yet). I thought perhaps I could use “shipping.selected” event but the object returned does not include the cart or buyer email.

Is there a JS event that is fired BEFORE the cart payment is made, but after the buyer email address has been added, that includes the cart details and the buyer details. If so I can use this to track carts on MailChimp before payment is made.

Many thanks

Hey @Roojai,

We don’t have an event for this, however, I’m pretty sure you could use our store API directly. This is documented here.

Everything from the current user, cart, and session is available in the store, we’re using Redux under the hood and we made it available for more complex scenarios.

let currentValue;
const unsubscribe = store.subscribe(() => {
    let previousValue = currentValue;
    currentValue = Snipcart.store.getState();

    if (previousValue !== currentValue) {
        // Make necessary checks and send data to MailChimp here.
    }
});

As you can see in the code above, you can compare the previous state with the latest, this will allow you to send events to MailChimp when you need to.

Let us know if that helps!

Hi Charles,

That should help. However I am getting an error “Uncaught ReferenceError: store is not defined”:

document.addEventListener('snipcart.ready', function() {
    let currentValue;
        const unsubscribe = store.subscribe(() => {
            let previousValue = currentValue;
            currentValue = Snipcart.store.getState();
        
            if (previousValue !== currentValue) {
                console.log("changed");
            }
        });

unsubscribe();
});

Not sure why that is. Any idea? The Snipcart.store.getState(); method works ok FYI.

Hi Roojai,

I just noticed there is a mistake in our documentation here is what you should use:

Snipcart.store.subscribe(()=>{

})

I will fix the documentation ASAP

1 Like