Search code examples
phpmagentoutm

Tracking from where customer add product to cart in Magento


I'm having a problem with Magento, I have several CMS blocks that add products to cart, some of them are promotion pages, other are like a product builder page that add several product (which are king of components to make other things) to the cart, and I'd like to know if the customers are using this pages, is it possible to like add a "utm" similar that would show on my backend saying "this user used this page/utm campaign/whatever" ?

It must have to do something with sessions, right? But other than that, I'm clueless.


Solution

  • You might want to look in to Google's Tagmanager for Analytics and DataLayers. There are all kinds of extensions available for this, though you could implement it yourself. I.e. look at this

    Using datalayers you can track each user over all pages, outputting the data you want, where you want it. It's a bit complex to wrap your head around, but it's definitely the best way to track and record your customers actions.

    I'm implementing this for the same reason at the moment, by calling a custom .phtml file in the head, and define the tags per page using PHP like below:

    <!-- Start GTM phtml -->
    
    <?php if(Mage::getURL('checkout/onepage/success') == Mage::helper('core/url')->getCurrentUrl()) { ?>
    <!-- GTM: Succes -->
    <script>
        dataLayer.push({
            'ecommerce': {
                'purchase': {
                    'actionField': {
                        'id': 'T12345',
                        'affiliation': 'xxx',
                        'revenue': 'xxx',
                        'tax': 'xxx',
                        'shipping': 'xxx'
                    },
                'products': [{
                    'name':'productname',
                    'id':'123',
                    'price':'25.95',
                    'brand':'brandname',
                    'category':'clothing',
                    'quantity':'1'
                    },
                    {
                    'name':'productname',
                    'id':'345',
                    'price':'10.95',
                    'brand':'brandname',
                    'category':'apparel',
                    'quantity':'2'
                    }]
                }
            }
        });
    </script>
    <?php } ?>
    
    <?php if(Mage::getURL('checkout/cart') == Mage::helper('core/url')->getCurrentUrl()) { ?>
    <!-- GTM: Cart -->
    <?php } ?>
    
    <?php if (strpos(Mage::helper('core/url')->getCurrentUrl(),'men') != false ) : ?>
    <!-- GTM: Men -->
    <?php endif; ?>
    
    <?php if($this->getRequest()->getControllerName()=='product') ://do something ?>
    <!-- GTM: All Products  -->
    <script>
        dataLayer.push({
            'event':'addToCart',
            'ecommerce': {
                'currencyCode':'EUR',
                'add':{
                    'products':[{
                        'name': 'Productname',
                        'id': '1234',
                        'price':'15.00'
                        'brand':'brandname'
                        'quantity':1
                    }]
                }
            }
        });
    </script>
    <?php endif; ?>
    
    <?php if (strpos(Mage::helper('core/url')->getCurrentUrl(),'woman/running') != false ) : ?>
    <!-- GTM: Woman Running -->
    <script>
        dataLayer.push({
            'ecommerce': {
                'purchase': {
                    'actionField': {
                        'id': 'T12345',
                        'affiliation': 'BK',
                        'revenue': 'BK',
                        'tax': 'BK',
                        'shipping': 'BK'
                    },
                'products': [{
                        'name': 'Productname',
                        'id': '1234',
                        'price':'15.00'
                        'brand':'brandname'
                        'quantity':1
                    },
                    {
                        'name': 'Productname',
                        'id': '1234',
                        'price':'15.00'
                        'brand':'brandname'
                        'quantity':1
                    }]
                }
            }
        });
    </script>
    <?php endif; ?>
    
    <!-- Google Tag Manager -->
        <noscript><iframe src="//www.googletagmanager.com/ns.html?id=XXX-XXXXXX"
        height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
        <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=
        '//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
        })(window,document,'script','dataLayer','XXX-XXXXXX');</script>
    <!-- End Google Tag Manager -->
    

    As you can see I use different checks to trigger the layers on specific pages. These can be exact urls, or urls containing a keyword (like 'man'). The idea is that you replace all the variables with the dynamic info from Magento ( echo $_product->getName(); and such)

    And a good place to start is, of course, the Google Docs itself