Skip to content

Instantly share code, notes, and snippets.

@jeroenfrenken
Last active August 25, 2023 13:21
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jeroenfrenken/4188d37681f8109a31490d82f191d518 to your computer and use it in GitHub Desktop.
Checkout end
<script>
(() => {
const configElement = document.getElementById("tagging-gtm-suite-config");
if (! configElement) {
console.error("Tagging Data Layer: Config element not found");
return;
}
const config = JSON.parse(configElement.textContent);
const script = document.createElement("script");
script.type = "text/javascript";
script.src = config.gtm_suite_script;
script.onerror = () => {
console.error("Tagging Data Layer: JS script failed to load");
};
script.onload = async () => {
if (!window.TaggingGtmSuite) {
console.error("Tagging Data Layer: `TaggingGtmSuite` is not defined");
return;
}
const cartData = {
attributes: {{- cart.attributes | json -}},
cartTotal: "{{- cart.total_price | times: 0.01 | json -}}",
currencyCode: {{- cart.currency.iso_code | json -}},
items: [{%- for line_item in cart.items -%}{
{%- if line_item.sku != blank -%}
id : {{- line_item.sku | json -}},
{%- else -%}
id : "{{- line_item.product_id | json -}}",
{%- endif -%}
name: {{- line_item.product.title | json -}},
brand: {{- line_item.vendor | json -}},
category: {{- line_item.product.type | json -}},
variant: {{- line_item.variant.title | json -}},
price: "{{- line_item.final_price | times: 0.01 | json -}}",
position: {{- forloop.index0 -}},
quantity: "{{- line_item.quantity | json -}}",
productId: "{{- line_item.product_id | json -}}",
variantId: "{{- line_item.variant_id -}}",
compareAtPrice: "{{- line_item.variant.compare_at_price | times: 0.01 | json -}}",
image: "{{- line_item.image | img_url -}}"
},{%- endfor -%}]
};
await window.TaggingGtmSuite.handlers.cartAttributesReconcile(cartData, config.event_config.save_order_notes, config.consent_enabled);
if (config.event_config.user) {
const data = {
{%- if checkout -%}
cartTotal : "{{- checkout.total_price | times: 0.01 | json -}}",
currencyCode : {{- checkout.currency | json -}},
{%- else -%}
cartTotal : "{{- cart.total_price | times: 0.01 | json -}}",
currencyCode : {{- cart.currency.iso_code | json -}},{%- endif -%}{%- if customer -%}customer : {
id: "{{- customer.id | json -}}",
email: {{- customer.email | json -}},
firstName: {{- customer.first_name | json -}},
lastName: {{- customer.last_name | json -}},
phone: {{- customer.phone | json -}},
city: {{- customer.default_address.city | json -}},
zip: {{- customer.default_address.zip | json -}},
address1: {{- customer.default_address.address1 | json -}},
address2: {{- customer.default_address.address2 | json -}},
country: {{- customer.default_address.country | json -}},
province: {{- customer.default_address.province | json -}},
provinceCode: {{- customer.default_address.province_code | json -}},
orderCount: "{{- customer.orders_count | json -}}",
totalSpent: "{{- customer.total_spent | times: 0.01 | json -}}",
tags: {{- customer.tags | join: ', ' | json -}}
}
{%- endif -%}
};
window.TaggingGtmSuite.handlers.user(data);
}
{%- if checkout -%}
const step = Shopify ?. Checkout ?. step;
const page = Shopify ?. Checkout ?. page;
const isOnBeginCheckout = step === "contact_information";
const isOnThankYouPage = page ?. includes("thank_you");{%- if checkout.attributes["_sld_visitor_info"] == blank -%}if (config.event_config.save_order_notes && Shopify ?. Checkout ?. token) {
if (isOnBeginCheckout) {
const data = {%- if checkout -%}
{
token: Shopify ?. Checkout ?. token,
currency: {{- checkout.currency | json -}},
{%- if customer -%}
customer : {
email: {{- customer.email | json -}}
},
{%- else -%}
customer : {},
{%- endif -%}
line_items: [{%- for line_item in checkout.line_items -%}{
id: {{- line_item.id | json -}},
quantity: {{- line_item.quantity | json -}},
title: {{- line_item.title | json -}},
variant: {
id: {{- line_item.variant.id | json -}},
price: {{- line_item.variant.price | json -}},
sku: {{- line_item.variant.sku | json -}},
title: {{- line_item.variant.title | json -}},
product: {
id: {{- line_item.variant.product.id | json -}},
title: {{- line_item.variant.product.title | json -}},
vendor: {{- line_item.variant.product.vendor | json -}}
}
}
},{%- endfor -%}],{%- if checkout.shipping_address -%}shipping_address : {
city: {{- checkout.shipping_address.city | json -}},
country: {{- checkout.shipping_address.country | json -}},
country_code: {{- checkout.shipping_address.country_code | json -}},
phone: {{- checkout.shipping_address.phone | json -}},
province: {{- checkout.shipping_address.province | json -}},
province_code: {{- checkout.shipping_address.province_code | json -}}
},
{%- else -%}
shipping_address : {},{%- endif -%}shipping_price: {{- checkout.shipping_price | json -}},
subtotal_price: {{- checkout.order.subtotal_price | json -}},
total_price: {{- checkout.total_price | json -}},
total_tax: {{- checkout.tax_price | json -}}
}
{%- endif -%};
window.TaggingGtmSuite.handlers.storeOrderNotesOffsite(data);
} else {
const data = {%- if checkout -%}
{checkoutId: {{- checkout.id | json -}}}{%- endif -%};
window.TaggingGtmSuite.handlers.storeOrderNotesOffsite(data);
}
}
{%- endif -%}
{% if request.path contains "thank_you" %}
if (config.event_config.checkout_complete && isOnThankYouPage) {
const data = {%- if checkout -%}
{
currencyCode: {{- checkout.currency | json -}},
actionField: {
{%- if order.id -%}
id : {{- order.id | json -}},
{%- else -%}
id : {{- checkout.id | json -}},
{%- endif -%}
{%- if order.name -%}
order_name : {{- order.name | json -}},
{%- endif -%}
affiliation: {{- shop.name | json -}},
revenue: "{{- checkout.total_price | times: 0.01 | json -}}",
tax: "{{- checkout.tax_price | times: 0.01 | json -}}",
shipping: "{{- checkout.shipping_price | times: 0.01 | json -}}",{% if checkout.discount_applications %}coupon : {{ checkout.discount_applications[0].title | json }},{% endif %}{%- if order.subtotal_price -%}subTotal : "{{- order.subtotal_price | times: 0.01 | json -}}",
{%- elsif checkout.subtotal_price -%}
subTotal : "{{- checkout.subtotal_price | times: 0.01 | json -}}",{%- endif -%}productSubTotal: "{{- checkout.line_items_subtotal_price | times: 0.01 | json -}}",
discountAmount: "{{- checkout.discounts_amount | times: 0.01 | json -}}"
},
{%- if checkout.customer -%}
customer : {
id: "{{- checkout.customer.id | json -}}",
email: {{- checkout.email | json -}},
firstName: {{- checkout.billing_address.first_name | json -}},
lastName: {{- checkout.billing_address.last_name | json -}},{%- if checkout.customer.phone -%}phone : {{- checkout.customer.phone | json -}},
{%- elsif checkout.billing_address.phone -%}
phone : {{- checkout.billing_address.phone | json -}},
{%- else -%}
phone : {{- checkout.shipping_address.phone | json -}},{%- endif -%}city: {{- checkout.billing_address.city | json -}},
zip: {{- checkout.billing_address.zip | json -}},
address1: {{- checkout.billing_address.address1 | json -}},
address2: {{- checkout.billing_address.address2 | json -}},
country: {{- checkout.billing_address.country | json -}},
province: {{- checkout.billing_address.province | json -}},
provinceCode: {{- checkout.billing_address.province_code | json -}},
orderCount: "{{- checkout.customer.orders_count | json -}}",
totalSpent: "{{- checkout.customer.total_spent | times: 0.01 | json -}}",
tags: {{- checkout.customer.tags | json -}}
},
{%- endif -%}
items: [{%- for line_item in checkout.line_items -%}{
id: {{- line_item.sku | json -}},
name: {{- line_item.product.title | json -}},
brand: {{- line_item.vendor | json -}},
category: {{- line_item.product.type | json -}},
variant: {{- line_item.variant.title | json -}},
price: "{{- line_item.final_price | times: 0.01 | json -}}",
quantity: "{{- line_item.quantity | json -}}",
productId: "{{- line_item.product_id | json -}}",
variantId: "{{- line_item.variant_id -}}",
image: "{{- line_item.image | img_url -}}"
},{%- endfor -%}],
landingSite: {{- checkout.landing_site | json -}}
}
{%- endif -%};
window.TaggingGtmSuite.handlers.checkoutComplete(data);
}
{%- endif -%}
if (Shopify ?. Checkout && ! isOnThankYouPage) {
const data = {%- if checkout -%}
{
currencyCode: {{- checkout.currency | json -}},
items: [{%- for line_item in checkout.line_items -%}{
id: {{- line_item.sku | json -}},
name: {{- line_item.product.title | json -}},
brand: {{- line_item.vendor | json -}},
category: {{- line_item.product.type | json -}},
variant: {{- line_item.variant.title | json -}},
price: "{{- line_item.final_price | times: 0.01 | json -}}",
quantity: "{{- line_item.quantity | json -}}",
productId: "{{- line_item.product_id | json -}}",
variantId: "{{- line_item.variant_id -}}",
compareAtPrice: "{{- line_item.variant.compare_at_price | times: 0.01 | json -}}",
image: "{{- line_item.image | img_url -}}"
},{%- endfor -%}]
}
{%- endif -%};
if (config.event_config.cart_reconcile) {
window.TaggingGtmSuite.handlers.cartItemsReconcile(data);
}
if (config.event_config.checkout_step) {
window.TaggingGtmSuite.handlers.checkoutStep(data);
}
}
{%- endif -%}
};
document.body.appendChild(script);
})();
</script>
[GTM_BODY_CODE]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment