Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
google tags
<!-- Google Tag Manager -->
<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-5FXXK2T');</script>
<!-- End Google Tag Manager -->
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-5FXXK2T"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
@yemenifree

This comment has been minimized.

Copy link
Owner Author

yemenifree commented Mar 3, 2019

Pages Name

add this code on each page

window.dataLayer.push({ 'page' : {pageName:'here page name'}});
PageName page
cart cart
payment checkout
productShow product details
purchase purchase
@yemenifree

This comment has been minimized.

Copy link
Owner Author

yemenifree commented Mar 3, 2019

Customer Info

If the customer is logged please push his data to dataLayer in all pages

<!-- Customer Information -->
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ 'customer' : {email: '<?php echo $customer_email;?>'}});

if the customer is guest add the follow code to header

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ 'customer' : {isGuest: true}});
@yemenifree

This comment has been minimized.

Copy link
Owner Author

yemenifree commented Mar 3, 2019

Add to cart

window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
  'event': 'addToCart',
  'ecommerce': {
    'currencyCode': 'SAR', // TODO :: add currency code
    'detail': { 
      'products': [{ // TODO :: add product infomation here
        'name': productObj.name,
        'id': productObj.product_id,
        'price': productObj.price.replace(/[^\d.-]/g, ''),
        'category': productObj.cat,
        'variant':  productObj.variant,
        'quantity': productObj.quantity
       }]
    }
  }
});
@yemenifree

This comment has been minimized.

Copy link
Owner Author

yemenifree commented Mar 3, 2019

Product Details

  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    'ecommerce': {
      "currencyCode": "SAR",
      "detail": {
        "products": [
          {
            "id": "123",
            "name": "product name",
            "categories": [
              {
                "id": 11189,
                "name": "category name"
              }
            ],
            "price": "10.00",
            "quantity": 78,
            "variant": "xl"
          }
        ]
      }
    }
  });
@yemenifree

This comment has been minimized.

Copy link
Owner Author

yemenifree commented Mar 3, 2019

Cart Page

  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
    'ecommerce': {
      "currencyCode": "SAR",
      "detail": {
        "products": [
          {
            "id": "123",
            "name": "product name",
            "categories": [
              {
                "id": 11189,
                "name": "category name"
              }
            ],
            "price": "10.00",
            "quantity": 78,
            "variant": "xl"
          },
          {
            "id": "123",
            "name": "product name",
            "categories": [
              {
                "id": 11189,
                "name": "category name"
              }
            ],
            "price": "10.00",
            "quantity": 78,
            "variant": "xl"
          }
        ]
      }
    }
  });
@yemenifree

This comment has been minimized.

Copy link
Owner Author

yemenifree commented Mar 3, 2019

Purchase/Checkout pages

  window.dataLayer = window.dataLayer || [];
  window.dataLayer.push({
  "ecommerce": {
    "currencyCode": "SAR",
    "detail": {
      "order": {
        "id": "order id",
        "total": 120,
        "coupon": "test coupone",
        "shipping_method": "Aramex",
        "payment": "COD"
      },
      "products": [
        {
          "id": "123",
          "name": "product name",
          "categories": [
            {
              "id": 11189,
              "name": "category name"
            }
          ],
          "price": "10.00",
          "quantity": 78,
          "variant": "xl"
        },
        {
          "id": "123",
          "name": "product name",
          "categories": [
            {
              "id": 11189,
              "name": "category name"
            }
          ],
          "price": "10.00",
          "quantity": 78,
          "variant": "xl"
        }
      ]
    }
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.