DataLayer for Google Tag Manager
How To Configure
DataLayer for Google Tag Manager

Affiliate sales tracking with Magento + Google Tag Manager + Google Analytics

Please Note: The settings below require our Datalayer for Google Tag Manager for Magento 2.

All Page View

Set cookie if user Affiliate user (site.com/?ies=abc123)

Tag Configuration
  Tag type : Custom Html

Trigger Configuration
  Trigger: All Pages

HTML

<script>
require(['DataLayerApi'], function (dataLayerApi) {
    var dataHelper = dataLayerApi();
    
    <!-- set cookie -->
    var urlKey = 'ies';
    var cookieName = 'affiliate_tracking';
    var lifetime = 60 * 60 * 24 * 30; // 30 days
    dataHelper.saveTrackingCodeIfKeyExist(urlKey, cookieName, lifetime);
});
</script>

To push the data to GA you can implement it in 1 or 2 ways

Purchase

####### Rewrite the data layer before sending the information to GA

Tag Configuration
  Tag type : Custom Html

Trigger Configuration
  Trigger: All Custom Events
  Event Name : purchase

HTML

<script>
require(['DataLayerPurchaseApi'], function (dataLayerPurchaseApi) {
    var purchase = dataLayerPurchaseApi();
    var cookieName = 'affiliate_tracking';
    var cookieValue = null;
    var data = {
      event: 'affiliatePurchase'  
    };
    
    if (cookieValue = purchase.getTrackingCodeIfKeyExist(cookieName, true)) {
        data.ecommerce =  {
            'purchase': {
                'actionField': {
                    'affiliation': cookieValue
                }
            }
        };
     }

    window.dataLayer.push(data);
});
</script>

Please Note: You will need to change your “purchase” event for tracking of order placement to ‘affiliatePurchase’, since it will take a few millisecond to load

Related

Related Extensions

Get more from your Magento2 store!

Order Shipment Tracking

Providing your customer with a beautiful and functional order package tracking without leaving your site.

Enhanced Success Page

Add cross-sell, upsell, related products, social media links and other elements to your order confirmation.

Catalog Hover Image for Magento

Quickly previewing alternative product images on your category list page.