Can anyone explain to me why I cannot seem to use a custom, customer events pixel to post data to an externally hosted server?
I have a simple script intended to post data to an external server:
analytics.subscribe('product_viewed', (event) => {
const product_data = {
attribute_one: “xxxxxxx”,
attribute_two: “xxxxxxx”,
data: event.data
};
fetch('https://mysecureendpoint.com/product_views, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(product_data),
keepalive: false,
})
….
});
But I consistently get errors like:
Access to fetch at ‘https://mysecureendpoint.com/product_views’ from origin ‘null’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.
If I make the same exact call outside of the customer events section, the request is successful and I receive no errors. No issues.
I’m using the same data, the same server & configurations, the same endpoint. The only difference is where the call takes place i.e. inside the customer events section vs directly on the product.liquid file.
This seems like a pretty obvious use case for the customer events utility. The external server, of course, is hosted on a different domain than the shopify store but is this intended behavior? What am I missing?
Thanks for any help!