Shopify Custom Pixel for Google Tag Manager

// Define dataLayer and the gtag function.
window.dataLayer = window.dataLayer || [];
function gtag() {
  dataLayer.push(arguments);
}

//Initialize GTM tag
(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-12345678');

//Google Consent Mode v2
gtag("consent", "update", {
  ad_storage: "granted",
  analytics_storage: "granted",
  ad_user_data: "granted",
  ad_personalization: "granted",
});

//subscribe to events
analytics.subscribe("page_viewed", (event) => {
  window.dataLayer.push({
    "event": "page_viewed",
    "client_id": event.clientId,
    "event_id": event.id,
    "timestamp": event.timestamp,
    "event_type": event.type,
    "page_location": event.context.document.location.href,
    "page_path": event.context.document.location.pathname,
    "hostname": event.context.document.location.hostname,
    "cookie_enabled": event.context.navigator.cookieEnabled,
    "language": event.context.navigator.language,
    "user_agent": event.context.navigator.userAgent,
  });
});

analytics.subscribe("product_viewed", (event) => {
  window.dataLayer.push({
    "event": "product_viewed",
    "client_id": event.clientId,
    "event_id": event.id,
    "timestamp": event.timestamp,
    "event_type": event.type,
    "ecommerce": {
      "value": event.data.productVariant.price.amount,
      "currency": event.data.productVariant.price.currencyCode,
      "items": [{        
        "item_id": item.sku,
        "item_name": item.title,
        "affiliation": item.product.vendor,
        "item_brand": item.product.vendor,
        "item_variant": item.title,    
        "price": item.price.amount,
        "quantity": item.quantity,
      }],
    },
  });
});

analytics.subscribe("checkout_completed", (event) => {
  const customer = init.data.customer;
  
  window.dataLayer.push({
    "event": "checkout_completed",
    "shopify_client_id": event.clientId,
    "shopify_event_id": event.id,
    "shopify_event_timestamp": event.timestamp,
    "page_location": event.context.window.location.href,
    "is_cookie_enabled": event.context.navigator.cookieEnabled,
    "browser_language": event.context.navigator.language,
    "user_agent": event.context.navigator.userAgent,
    "url_hash": event.context.window.location.hash,
    "hostname": event.context.window.location.hostname,
    "url_path": event.context.window.location.pathname,
    "url_parameters_string": event.context.window.location.search,
    "customer_email": event.data.checkout.email,
    "customer_phone_number": event.data.checkout.phone,
    "checkout_brutto_price": event.data.checkout.subtotalPrice.amount,
    "checkout_netto_price": event.data.checkout.totalPrice.amount,
    "checkout_token": event.data.checkout.token,
    "checkout_payment_provider": event.data.checkout.transactions.gateway,
    "billing_address": event.data.checkout.billingAddress.address1 + " " + event.data.checkout.billingAddress.address2,
    "billing_city": event.data.checkout.billingAddress.city,
    "billing_country": event.data.checkout.billingAddress.country,
    "billing_country_code": event.data.checkout.billingAddress.countryCode,
    "billing_province": event.data.checkout.billingAddress.province,
    "billing_province_code": event.data.checkout.billingAddress.provinceCode,
    "billing_first_name": event.data.checkout.billingAddress.firstName,
    "billing_last_name": event.data.checkout.billingAddress.lastName,
    "shipping_phone_number": event.data.checkout.shippingAddress.phone,
    "billing_zip": event.data.checkout.billingAddress.zip,
    "customer_id": event.data.checkout.order?.customer?.id,
    "initiated_customer_id": customer?.id,
    "new_customer": !event.data.checkout.order?.customer?.id ? true : false,
    "order_count": customer?.ordersCount,
    // "new_customer": !customer || customer?.ordersCount < 1 ? true : false,
    "ecommerce": {
      "transaction_id": event.data.checkout.order.id,
      "value": event.data.checkout.subtotalPrice.amount,
      "currency": event.data.checkout.currencyCode,
      "coupon": event.data.checkout.discountApplications.title,
      "shipping": event.data.checkout.shippingLine.price.amount,
      "tax": event.data.checkout.totalTax.amount,
      "items": event.data.checkout.lineItems.map(item => ({
        "item_id": 'shopify_US_' + item.variant.product.id + '_' + item.variant.id,
        "item_name": item.variant.product.title + ' - ' + item.variant.title,
        "affiliation": item.variant.product.vendor,
        "item_brand": item.variant.product.vendor,
        "item_variant": item.variant.title,    
        "price": item.variant.price.amount,
        "quantity": item.quantity,
      }))
    },
  });
});