Using a custom Google Analytics code with Buy Button

Highlighted
New Member
2 0 0

I tried this solution but for some reason when I edit the domain name the button disappears. Anyone know how to fix this?

 

0 Likes
Highlighted
Excursionist
14 1 2

Did any of you guys figured out how to get the orders through with the decimals. It's showing orders from 30,00 as 3000. Would love to have this fixed.

0 Likes
Highlighted

Hi all,

 

The key to getting this working 100% is to use the liquid template code, here is my code that s added to the Shopify 'Additional scripts' in checkout. Getting the correct currency output is achieved by using:

{{ checkout.subtotal_price | money_without_currency }}

You also need to wrap the <script> tag inside {% if first_time_accessed %} {% endif %} so if a user accesses their order at a later date the order is not tracked again.

 

I can't seem to get the category title to output using {{ line_item.product.collections[0].title }} but the rest of it works.

 

Here is the full code:

 

{% if first_time_accessed %}
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXX-1"></script>
<script>
    window.dataLayer = window.dataLayer || [];

    function gtag() {
        dataLayer.push(arguments);
    }
    gtag('set', 'linker', {
        'domains': ['exampledomain.co.nz']
    });
    gtag('js', new Date());

    gtag('config', 'UA-XXXXXX-1');
    gtag('event', 'purchase', {
        transaction_id: {{ checkout.order_number }},
        value: {{ checkout.subtotal_price | money_without_currency }},
        currency: "{{ shop.currency }}",
        tax: "{{ checkout.tax_price|money_without_currency }}",
        shipping: {{ checkout.shipping_price | money_without_currency }},
        items: [
            {% for line_item in line_items %}
            {% assign list_position = forloop.index + 1 %}
            {
                "id": "{% if line_item.sku != blank %}{{ line_item.sku }}{% else %}{{ line_item.product.handle }}{% endif %}",
                "name": "{{line_item.title}}",
                "category": "{{ line_item.product.collections[0].title }}",
                "list_position": "{{ list_position }}",
                "quantity": "{{ line_item.quantity }}",
                "price": "{{ line_item.line_price | money_without_currency}}"
            }
            {% unless forloop.last %},{% endunless %}
            {% endfor %}
        ]
    });
</script>
{% endif %}

Enjoy.

1 Like
Highlighted
Excursionist
14 1 2
Wow! thanks, gonna try this out!
0 Likes
Highlighted
Excursionist
14 1 2

Hi Swordfox,

 

For me the price sadly still comes without the , on the right place. So an order for 49,99 comes in as 4999,-

0 Likes
Highlighted

If your store formats its currency with a comma often seen in European currencies, monetary values in the code need to have the filter changed like {{ checkout.total_price | money_without_currency | replace: ',', '.' }}

Run Google Shopping ads? Get the free definitive guide to Google Shopping for Shopify (no optin required): https://www.digitaldarts.com.au/google-shopping
2 Likes