Skip to content

Instantly share code, notes, and snippets.

@raselupm
Last active August 13, 2024 05:22
Show Gist options
  • Save raselupm/af177f5475cb7dff2f71cfcfef5e09f0 to your computer and use it in GitHub Desktop.
Save raselupm/af177f5475cb7dff2f71cfcfef5e09f0 to your computer and use it in GitHub Desktop.
BV checkout scripts
<script>
window.dataLayer = window.dataLayer || [];
</script>
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({"gtm.start":
new Date().getTime(),event:"gtm.js"});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!="dataLayer"?"&l="+l:"";j.async=true;j.src=
"https://www.googletagmanager.com/gtm.js?id="+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,"script","dataLayer","GTM-NQ49BKQ");
</script>
<!-- End Google Tag Manager -->
<script id="elevar-gtm-suite-config" type="application/json">{"gtm_id": "GTM-NQ49BKQ", "event_config": {"cart_reconcile": true, "cart_view": true, "checkout_complete": true, "checkout_step": true, "collection_view": true, "product_add_to_cart": false, "product_add_to_cart_ajax": true, "product_remove_from_cart": true, "product_select": true, "product_view": true, "search_results_view": true, "user": true, "save_order_notes": true}, "gtm_suite_script": "https://cdn.shopify.com/s/files/1/0667/4766/2632/files/Shopify_Datalayer.js", "consent_enabled": false}</script>
{% if first_time_accessed %}
<script>
(() => {
const configElement = document.getElementById("elevar-gtm-suite-config");
if (!configElement) {
console.error("Elevar 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("Elevar Data Layer: JS script failed to load");
};
script.onload = async () => {
if (!window.ElevarGtmSuite) {
console.error("Elevar Data Layer: `ElevarGtmSuite` 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.ElevarGtmSuite.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.ElevarGtmSuite.handlers.user(data);
}
{%- if checkout -%}
{%- if checkout.attributes["_elevar_visitor_info"] == blank -%}
if (config.event_config.save_order_notes) {
const data = {%- if checkout -%}
{
checkoutId: {{- checkout.id | json -}}
}
{%- endif -%}
;
window.ElevarGtmSuite.handlers.storeOrderNotesOffsite(data);
}
{%- endif -%}
if (config.event_config.checkout_complete) {
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.ElevarGtmSuite.handlers.checkoutComplete(data);
}
{%- endif -%}
};
document.body.appendChild(script);
})();
</script>
{% endif %}
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-NQ49BKQ" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
<!-- Entaice order-processing script -->
<script type="text/javascript">
window.addEventListener('load', function() {
window.dataLayer = window.dataLayer || [];
function getThemeDetails() {
{% if theme %}
return [{{ theme.id }}, "{{ theme.name | escape }}"];
{% else %}
return [-1, "no name"];
{% endif %}
}
function getCustomerDetails() {
{% if customer %}
return [{{ customer.id }}, "{{ customer.email | escape }}"];
{% else %}
return [-1, "no email"];
{% endif %}
}
var themeDetails = getThemeDetails();
var customerDetails = getCustomerDetails();
window.dataLayer.push({
entaice_gen: {
shop_domain: {% if shop.permanent_domain %} "{{ shop.permanent_domain | escape }}" {% else %} "" {% endif %},
theme_id: themeDetails[0],
theme_name: themeDetails[1],
page_type: "Thank You Page",
customer_id: customerDetails[0],
customer_email: customerDetails[1]
}
});
{% if order %}
var lineItems = {{ order.line_items | json }}.map(function(item, idx) {
return {
id: item.id,
product_id: item.product.id,
product_title: (item.product.title || "").toString(),
variant_id: item.variant_id,
variant_title: (item.variant.title || "").toString(),
sku: (item.sku || "").toString(),
quantity: item.quantity,
price: item.line_price * 0.01,
properties: (("properties" in item ? item.properties : []) || []),
option_1: (item.variant.option1 || "").toString(),
option_2: (item.variant.option2 || "").toString(),
option_3: (item.variant.option3 || "").toString(),
inventory_quantity: item.variant.inventory_quantity,
discounts: item.applied_discounts
};
});
window.dataLayer.push({
entaice_order: {
has_data: 1,
id: {{ order.order_number }},
created_at: "{{ order.created_at }}",
email: "{{ order.email }}",
subtotal_price: {{ order.subtotal_price | times: 0.01 }},
shipping_price: {{ order.shipping_price | times: 0.01 }},
tax_price: {{ order.tax_price | times: 0.01 }},
line_items: JSON.stringify(lineItems)
},
event: "entaice_order"
});
{% else %}
window.dataLayer.push({
entaice_order: {
has_data: 0
},
event: "entaice_order"
});
{% endif %}
}, false);
</script>
<!-- End Entaice order-processing script -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment