Retrouver en dessous de la video le code du custom_purchase
https://www.youtube.com/watch?v=9LlsBwHDaow
<script>
window.dataLayer = window.dataLayer || [];
{% if customer %}
dataLayer.push({
'event': 'user_data',
'ecommerce':{
'currency': {{ shop.currency | json }},
},
'user_data': {
'user_id': "{{ customer.id | default: undefined }}",
'email_address': "{{ customer.email | default: undefined }}",
'address': {
{% if customer.default_address %}
'city': "{{ customer.default_address.city | default: undefined }}",
'address': "{{ customer.default_address.address1 | default: undefined }} {{ customer.default_address.address2 | default: undefined }}",
'state': "{{ customer.default_address.province | default: undefined }}",
'country': "{{ customer.default_address.country | default: undefined }}",
'postal_code': "{{ customer.default_address.zip | default: undefined }}",
'first_name': "{{ customer.default_address.first_name | default: undefined }}",
'last_name': "{{ customer.default_address.last_name | default: undefined }}"
{% else %}
'first_name': "{{ customer.first_name | default: undefined }}",
'last_name': "{{ customer.last_name | default: undefined }}"
{% endif %}
}
}
});
{% endif %}
</script>
{% if first_time_accessed %}
<script>
{% if customer %}
var orders_count = '{{ customer.orders_count }}'
var total_spent = '{{ customer.total_spent }}'
{% endif %}
window.dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
var transaction_id = '{{ order_number }}' || undefined ;
var value = {{ checkout.subtotal_price | divided_by: 100.0 }} || 0;
var shipping = {{ order.shipping_price | divided_by: 100.0 }} || 0;
var tax = {{ order.tax_price | divided_by: 100.0 }} || 0;
var coupon_code = Shopify.checkout.discount?.code || '';
var currency = '{{ currency }}' || 'USD';
var line_items = [];
Shopify.checkout.line_items.forEach((product_line_item, index) => {
line_items.push({
'item_id': product_line_item.product_id || undefined ,
'item_sku': product_line_item.sku || undefined ,
'item_name': product_line_item.title || undefined ,
'affiliation': Shopify.shop || undefined ,
'price': product_line_item.price || 0,
'item_brand': product_line_item.vendor || undefined ,
'item_variant': product_line_item.variant_title || undefined ,
'item_variant_id': product_line_item.variant_id || undefined ,
'quantity': product_line_item.quantity || 1,
'currency': currency,
'index': index,
'google_business_vertical': 'retail'
});
});
dataLayer.push({
'event': 'custom_purchase',
'datalayer_event_name': "purchase",
'ecommerce': {
'timestamp': Shopify.checkout.created_at,
'transaction_id': transaction_id,
'value': value,
'orders_count': orders_count || undefined,
'total_spent' : total_spent || undefined,
'shipping': shipping,
'tax': tax,
'coupon': coupon_code,
'currency': currency,
'discount_amount': Shopify.checkout?.discount?.amount || undefined,
'items': line_items,
},
});
</script>
{% endif %}