Shopify Pixel Integration
Connect usertrax with your Shopify store to automatically track conversions.
Setup in Shopify
Section titled “Setup in Shopify”Create Pixel in Shopify Admin
Open Shopify Admin and navigate to “Settings” → “Customer Events”. Click on “Add Custom Pixel”
Configure Pixel
Set the Name to “usertrax Conversion Tracking”
Insert Pixel Code
Copy the following code into the Pixel Code field in Shopify:
/*** Shopify Pixel for usertrax* Loads usertrax and pushes events.* Compatible with Shopify's Custom Pixel requirements*/(function () {"use strict";// usertrax Configurationconst USERTRAX_CONFIG = {scriptUrl: "https://usertrax.io/cvs.js", // Replace with your usertrax API URLpublicKey: "YOUR_API_KEY", // Replace with your usertrax Public Key// Shopify-specific ConfigurationshopifyEvents: [// "page_viewed",// "clicked",// "form_submitted","product_viewed","product_added_to_cart","cart_viewed","checkout_started","checkout_completed",],};// Load usertrax Trackerfunction loadUsertrax() {return new Promise((resolve, reject) => {// Check if usertrax is already loadedif (window.usertrax) {resolve();return;}const script = document.createElement("script");script.src = USERTRAX_CONFIG.scriptUrl;script.setAttribute("data-key", USERTRAX_CONFIG.publicKey);script.onload = () => resolve();script.onerror = () => reject(new Error("Failed to load usertrax"));document.head.appendChild(script);});}// Send event to usertraxasync function sendConversion(conversionData) {try {window.usertrax.push(conversionData);return true;} catch (error) {console.error("Error sending conversion to usertrax:", error);return false;}}// Shopify Event Handlerfunction handleShopifyEvent(eventName, event) {console.log(`Shopify Event: ${eventName}`, event);let conversionData = {event: eventName,meta_data: {shopify: {shop: window.Shopify?.shop || window.location.hostname,currency: window.Shopify?.currency || "EUR",locale: window.Shopify?.locale || "en",},},};// Process event-specific dataswitch (eventName) {case "checkout_completed":conversionData = {...conversionData,id: event.checkout?.order_id || event.order_id,total: parseFloat(event.checkout?.total_price || event.total_price || 0,),currency: event.checkout?.currency || event.currency || "EUR",user_data: {email: event.checkout?.email || event.email,id: event.checkout?.customer_id || event.customer_id,token: event.checkout?.token,firstname:event.checkout?.customer_first_name || event.first_name,lastname: event.checkout?.customer_last_name || event.last_name,phone: event.checkout?.customer_phone || event.phone,address: event.checkout?.customer_address || event.address,city: event.checkout?.customer_city || event.city,state: event.checkout?.customer_state || event.state,zip: event.checkout?.customer_zip || event.zip,country: event.checkout?.customer_country || event.country,},};conversionData.meta_data.shopify = {...conversionData.meta_data.shopify,order_id: event.checkout?.order_id || event.order_id,checkout_id: event.checkout?.id || event.checkout_id,line_items: event.checkout?.line_items || event.line_items,};break;case "product_viewed":conversionData = {...conversionData,id: event.product?.id || event.product_id,label: event.product?.title || event.product_title,total: parseFloat(event.product?.price || event.product_price || 0,),};conversionData.meta_data.shopify = {...conversionData.meta_data.shopify,product_id: event.product?.id || event.product_id,product_title: event.product?.title || event.product_title,product_price: parseFloat(event.product?.price || event.product_price || 0,),product_variant_id:event.product?.variant_id || event.product_variant_id,product_category:event.product?.category || event.product_category,};break;case "product_added_to_cart":conversionData = {...conversionData,id: event.product?.id || event.product_id,label: event.product?.title || event.product_title,total: parseFloat(event.product?.price || event.product_price || 0,),};conversionData.meta_data.shopify = {...conversionData.meta_data.shopify,cart_id: event.cart?.id || event.cart_id,cart_token: event.cart?.token || event.cart_token,product_id: event.product?.id || event.product_id,product_title: event.product?.title || event.product_title,product_price: parseFloat(event.product?.price || event.product_price || 0,),product_variant_id:event.product?.variant_id || event.product_variant_id,product_category:event.product?.category || event.product_category,};break;case "checkout_started":conversionData = {...conversionData,total: parseFloat(event.cart?.total_price || event.cart_total || 0,),currency: event.cart?.currency || event.currency || "EUR",};break;}sendConversion(conversionData);}// Listen to Shopify Analytics Eventsfunction listenToShopifyAnalytics() {// Wait for Shopify Analytics to be availableconsole.log("Shopify Analytics found, subscribing to events...");// Subscribe to all configured eventsUSERTRAX_CONFIG.shopifyEvents.forEach((eventName) => {try {analytics.subscribe(eventName, (event) => {handleShopifyEvent(eventName, event);});console.log(`Event subscribed: ${eventName}`);} catch (error) {console.warn(`Could not subscribe to event ${eventName}:`, error);}});}// Initializationasync function init() {try {console.log("Initializing usertrax Shopify Pixel...");// Load usertraxawait loadUsertrax();console.log("Usertrax successfully loaded");// Listen to Shopify Analytics EventslistenToShopifyAnalytics();// Pixel successfully loadedconsole.log("Usertrax Shopify Pixel successfully initialized");// Event for other scriptswindow.dispatchEvent(new CustomEvent("usertrax:ready", {detail: { config: USERTRAX_CONFIG },}),);} catch (error) {console.error("Error initializing usertrax Shopify Pixel:", error);// Event for errorswindow.dispatchEvent(new CustomEvent("usertrax:error", {detail: { error: error.message },}),);}}init();// Expose for external accesswindow.UsertraxPixel = {config: USERTRAX_CONFIG,sendConversion,init,};})();Configure API Key
Replace
YOUR_API_KEYin the code with your actual usertrax API key from your dashboard.Activate Pixel
- Save: Click “Save”
- Activate: Make sure the pixel is activated
Tracked Events
Section titled “Tracked Events”The usertrax Shopify pixel automatically tracks the following events:
- product_viewed: Product views
- product_added_to_cart: Products added to cart
- cart_viewed: Cart viewed
- checkout_started: Checkout process started
- checkout_completed: Order completed (conversion)
Troubleshooting
Section titled “Troubleshooting”- Events not being sent: Check the browser console for errors
- Wrong API key: Make sure the correct API key is being used
- Pixel not active: Check in Shopify Admin if the pixel is activated