Hi everyone, university student here building a Shopify app as a senior project. I have a web pixel extension that subscribes to the product_viewed event. I want to post some data from that event to my Shopify app.
The current route I’m trying is using an app proxy, but I’m running in to a restrictedURL error when I try to post the data:
RestrictedUrlError: Requests are not allowed to the same origin: https://tosh-dev-story.myshopify.com/apps/collect
Does anyone have any input on the best pattern to follow for posting this data to my Shopify app?
Here is the current web pixel extension code that is trying that request:
register(({ analytics, browser, init, settings }) => {
// Get shop from the current page URL or from pixel context
const shopDomain = init?.context?.document?.location?.hostname;
const collectUrl = `https://${shopDomain}/apps/collect`;
analytics.subscribe("product_viewed", (event) => {
let payload = {
client_id: event.clientId,
timestamp: event.timestamp,
product: event.data.productVariant,
url: event.context.document.location.href,
};
console.log("[Pixel] product viewed ", payload);
// Post data to server. Does not handle any response, it is up to the server to validate and handle the data.
fetch(collectUrl, {
method: "POST",
mode: "no-cors",
headers: {
"Content-Type": "application/json", // Indicate that the body is JSON
},
body: JSON.stringify(payload),
});
});
}
I have the app proxy set up in my shopify.app.toml file:
[app_proxy]
url = "/api/collect"
prefix = "apps"
subpath = "collect"
I have also tried re-installing the app as I understand the app proxy is set up at installation time.
Thanks in advance!