How can I effectively track purchase events with Twitter Pixel?

maxrfinch
Shopify Partner
34 1 48

Hello. I'm trying to get a post-purchase pixel working to track purchase events using the Twitter Pixel. I've added the following script to the post-purchase script section in Shopify (we have not upgraded to checkout extensibility). 

 

 

 

<script type="text/javascript">
(function() {
  var order = window.Shopify.order;
  var currency = order.currency;

  // Total value of the order
  var orderValue = order.totalPrice;

  var contents = order.lineItems.map(function(item) {
    return {
      content_type: 'product',
      content_id: item.id,
      content_name: item.title,
      content_price: item.price, // Price in the currency of the shop
      quantity: item.quantity
    };
  });

  // Trigger the Twitter Pixel event
  twq('event', '[event-name-redacted]', {
    value: orderValue,
    currency: currency,
    contents: contents,
    conversion_id: order.number, 
    email_address: order.customer.email, 
    phone_number: order.customer.phone
  });

})();
</script>

 

 

Am I approaching this the right way? I know there is a customer events section in Shopify for custom pixels, but I have not dove into that. 

Replies 3 (3)

maxrfinch
Shopify Partner
34 1 48

In addition, I've attempted to add the pixel tracking via Shopify's custom pixels, and it works for my add to cart event, but not my purchase events. 

 

 

// Step 1. Initialize the JavaScript pixel SDK (make sure to exclude HTML)
!function(e,t,n,s,u,a){e.twq||(s=e.twq=function(){s.exe?s.exe.apply(s,arguments):s.queue.push(arguments);
},s.version='1.1',s.queue=[]);var f=t.createElement(n),h=t.getElementsByTagName(n)[0];
f.async=!0,f.src='https://static.ads-twitter.com/uwt.js',h.parentNode.insertBefore(f,h)}(window,document,'script');
twq('init','[base-pixel-redacted]'); // Initialize Twitter Pixel with your base pixel ID

// Step 2. Subscribe to customer events with analytics.subscribe(), and add tracking
// Track Cart Additions
analytics.subscribe("cart_addition", event => {
  twq('track','[cart-id-redacted]', { 
    content_ids: event.data.product_ids,
    content_type: 'product',
    value: event.data.total_price,
    currency: 'USD'
  });
});

// Track Purchases
analytics.subscribe("checkout_completed", event => {
  twq('track','[purchase-id-redacted]', { 
    value: event.data.checkout.totalPrice.amount, 
    currency: event.data.checkout.currencyCode,
    email_address: event.data.checkout.email 
  });
});

 

 

 

Letty12345
Visitor
2 0 0

Hello!
Did you find the solution? I have the same issue!
Thanks a lot,

Letty

ajsmith227
New Member
5 0 0

Have you had any luck? Suck as well