X ads pixel code for Purchase Event

Topic summary

A developer is struggling to track purchase events with X (Twitter) ads pixel code. Site visits are working correctly, but purchase data isn’t being sent back to X ads.

Code Issues Identified:

  • The JavaScript code appears corrupted or reversed in several sections
  • Variable references for contents, conversion_id, and phone_number look problematic
  • These values need to be properly defined as variables earlier in the code

Suggested Troubleshooting:

  • Verify the correct notation for order_id (should reference event.data.checkout.order.id)
  • Add console.log statements within the subscribe function to debug
  • Test whether the checkout completion event fires for non-Twitter scripts
  • Check if variables are properly initialized before the pixel tracking call

The issue remains unresolved and requires code correction and debugging to properly capture purchase event data.

Summarized with AI on November 8. AI used: claude-sonnet-4-5-20250929.

Having a hard getting purchase info back to X ads with pixel code. Site vists/lands working fine. See anything wrong in my code?

!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=[],u=t.createElement(n),u.async=!0,u.src="https://static.ads-twitter.com/uwt.js",
a=t.getElementsByTagName(n)[0],a.parentNode.insertBefore(u,a))}(window,document,"script");

twq("config", "myaccount");

analytics.subscribe("checkout_completed", (event) => {
  twq('event', 'purchasecode', {
    'value': event.data.checkout.subtotalPrice.amount,
    'currency': event.data.checkout.currencyCode,
    'email_address': event.data.checkout.email,
    'contents': contents,
    'conversion_id': order.number, 
    'phone_number': order.customer.phone
  });
});

The contents, conversion_id, phone_number values seem sketchy unless you defined them as variables earlier in the code. For example, the correct notation for the order_id should be

'conversion_id': event.data.checkout.order.id

Second of all: Does this event work for other non-twitter scripts? You could maybe check with a console.log in the subscribe function and a test order.