Hi,
I have created a custom web pixel that should trigger after checkout completed. The result is our review widget is shown and customers are invited to place a review. But apperently a custom pixel can’t project a widget. Is that correct? I read somewhere that shopify might implement this possibility? Or does anyone have another solution?
This is the code:
analytics.subscribe(‘checkout_completed’, (event) => {
console.log(“Trusted Shops - Checkout Completed Event Fired!”);
const checkoutData = event.data.checkout || {};
const orderNumber = checkoutData.order?.id || “Unknown”;
const email = checkoutData.email || “Unknown”;
const totalPrice = checkoutData.totalPrice?.amount || “0.00”;
const currency = checkoutData.currencyCode || “EUR”;
const paymentMethod = checkoutData.transactions?.[0]?.paymentMethod || “Unknown”;
const lineItems = checkoutData.lineItems || checkoutData.items || ;
console.log(“Trusted Shops Order Data Captured:”, {
orderNumber, email, totalPrice, paymentMethod, currency, lineItems
});
// Language Detection Fix
let tsLanguage = document.documentElement.lang?.trim() || “”;
if (!tsLanguage) {
const urlParams = new URLSearchParams(window.location.search);
tsLanguage = urlParams.get(“locale”) || “”;
}
tsLanguage = tsLanguage.toLowerCase().substring(0, 2);
console.log( Detected Language: "${tsLanguage}");
let tsid = “empty”;
if (tsLanguage === “de”) {
tsid = “xxx”;
} else if (tsLanguage === “nl”) {
tsid = “xxx”;
} else if (tsLanguage === “fr”) {
tsid = “xxx”;
}
if (tsid !== “empty”) {
console.log( Injecting Trusted Shops widget for language: ${tsLanguage});
let script = document.createElement(“script”);
script.type = “text/javascript”;
script.charset = “utf-8”;
script.async = true;
script.src=//widgets.trustedshops.com/js/${tsid}.js;
script.onload = function () {
console.log(" Trusted Shops script loaded successfully.");
// Wait for Trusted Shops to be available (check every 500ms)
let attempts = 0;
const maxAttempts = 10;
const checkTSObject = setInterval(() => {
if (window.TrustedShops) {
clearInterval(checkTSObject);
console.log(" Trusted Shops Widget Found - Initializing…");
try {
window.TrustedShops.init(); // Initialize Trusted Shops
console.log(" Trusted Shops Widget Successfully Initialized.“);
} catch (error) {
console.error(” Failed to initialize Trusted Shops Widget:", error);
}
} else {
attempts++;
console.warn( Trusted Shops object not found (Attempt ${attempts}/${maxAttempts}));
if (attempts >= maxAttempts) {
clearInterval(checkTSObject);
console.error(" Trusted Shops object not available after multiple attempts.");
}
}
}, 500); // Check every 500ms
};
script.onerror = function () {
console.error(" Trusted Shops script failed to load.");
};
document.body.appendChild(script);
} else {
console.warn( No Trusted Shops widget loaded. Detected language: "${tsLanguage}");
}
});