Google Ads Conversion Tracking - Subtask 5 (Bonus)
Description
Bonus Shopify has a piece of code that only allows a script to fire the first time a page is accessed, can you
add that as well?
- Add code to ensure script only fires the first time a page is accessed.
Solution
Encapsulate the event snippet with the following Liquid first_time_accessed property: {% if first_time_accessed %} and {% endif %}.
{% if first_time_accessed %}
<!-- Event snippet for Test conversion page -->
<script>
gtag('event', 'conversion', {
'send_to': 'AW-123456789/AbC-D_efG-h12_34-567',
'value': {{ subtotal_price | money_without_currency }},
'currency': '{{ currency }}',
'transaction_id': '{{ transaction_id }}'
});
</script>
{% endif %}
Explanation
Actually, we already addressed this in Subtask 4 where a more detailed explanation is provided. To reiterate, surrounding the conversion event code snippet, or any code for that matter, with {% if first_time_accessed %} and {% endif %} ensures the code only runs once and prevents capture of duplicate conversions.