Main issue: how to add onclick conversion tracking to âBuy nowâ and PayPal buttons in the Shopify Dawn theme.
Key context:
In Dawn, these dynamic checkout buttons are rendered in /snippets/buy-buttons.liquid. They use {{ form | payment_button }}, so you canât directly add custom HTML attributes like onclick.
A screenshot (external link) contains the exact tracking code to be fired on click.
Proposed solution:
Add JavaScript in the main product section to attach an onclick handler at runtime.
Select the dynamic checkout button via its data-testid attribute (e.g., [data-testid=âCheckout-buttonâ]).
Use setAttribute to inject the required onclick code (e.g., mgq.push([âŚ]) instead of console.log).
Implementation details:
Place the script above the closing tag in the main product section (main-product.liquid).
Example approach was demonstrated via screencast; the provided mgq.push example is confirmed as correctly placed.
Status and next steps:
Resolution reached: use JS to add onclick to dynamic checkout buttons.
Recommendation: test on an unpublished theme and verify events in the analytics tool before publishing.
Where to add conversion tracking code would typically depend on the theme youâre using. Which theme are you using and which button specifically do you want to add the conversion tracking to?
However the options for customizing dynamic checkout buttons is quite limited as the button is rendered via Liquid: {{ form | payment_button }} rather than as a regular HTML element - so you donât have easy access from the code to add a custom class/ID, or in your case custom code that measures clicks/conversions.
What are the exact instructions for adding onclick code to count conversions?
I have tested out how to add an onclick Event Attribute to the dynamic checkout button, and it appears to be possible by adding Javascript to the main product section.
What youâd need to do is use a querySelector method to select the dynamic checkout button element using the data-testid attribute which is added to dynamic checkout buttons. The setAttribute method is then used to add the onclick event attribute to the button element.
In the example below Iâm adding an onclick event attribute to the dynamic checkout element, so every time itâs clicked it will log a message to the console:
You can see this in action in this screencast. You can use the same approach but add in the function/ code you were provided in place of the console.log function in my example.
Thank you for the idea. Let me specify just to be 100% sure. I have to add a modified function to main-product.liquid file, correct?
Where exactly do I need to add it to the code?
Please check the code below, is that what you meant by âadd code to the functionâ?
Hi! That does look like correct placement of the function you were provided - this would be added to the main product section above the closing tag. Itâs also advised to test this on an unpublished theme and ensure the expected events are being triggered on the web analysis tool before publishing the changes.