1

I was wondering if someone could write a generic guide to develop a pixel tracking module.

1

Here's a generic guide you can write to develop a pixel tracking module. As it is generic you will have to adapt that code (specially the template code) to match your requirements:

app/etc/modules/DigitalPianism_PixelTracking.xml

<?xml version="1.0"?>
<config>
    <modules>
        <DigitalPianism_PixelTracking>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Checkout />
            </depends>
        </DigitalPianism_PixelTracking>
    </modules>
</config>

app/code/local/DigitalPianism/PixelTracking/etc/config.xml

<?xml version="1.0"?>
<config>

    <modules>
        <DigitalPianism_PixelTracking>
            <version>0.0.1</version>
        </DigitalPianism_PixelTracking>
    </modules>

    <frontend>
        <layout>
            <updates>
                <pixeltracking module="DigitalPianism_PixelTracking">
                    <file>digitalpianism/pixeltracking.xml</file>
                </pixeltracking>
            </updates>
        </layout>
    </frontend>

    <global>
        <blocks>
            <pixeltracking>
                <class>DigitalPianism_PixelTracking_Block</class>
            </pixeltracking>
        </blocks>
        <helpers>
            <pixeltracking>
                <class>DigitalPianism_PixelTracking_Helper</class>
            </pixeltracking>
        </helpers>
    </global>

</config>

(Optional) app/code/local/DigitalPianism/PixelTracking/etc/system.xml

Depending on your requirements you may want to create a system configuration fields file in order to make your tracking configurable (for example you can add tracking URL, API key, a switch to enable/disable the tracking)

(Optional) app/code/local/DigitalPianism/PixelTracking/etc/adminhtml.xml

If you add a new configuration section you will need this file for the ACL.

app/code/local/DigitalPianism/PixelTracking/Helper/Data.php

<?php

class DigitalPianism_PixelTracking_Helper_Data extends Mage_Core_Helper_Abstract 
{
}

NB: if you have added system configuration fields you can create the corresponding utility methods here to be able to retrieve the value via the helper.

app/design/frontend/base/default/layout/digitalpianism/pixeltracking.xml

<?xml version="1.0"?>
<layout version="0.1.0">
    <checkout_onepage_success>
        <reference name="before_body_end">
            <block type="pixeltracking/tracker" name="pixeltracking" template="digitalpianism/pixeltracking/tracker.phtml"/>
        </reference>
    </checkout_onepage_success>
</layout>

Now it's time to create your block class. It will contain all the methods necessary for your tracking, the example below is a generic example but you can add whatever methods you need in your template:

<?php

class DigitalPianism_PixelTracking_Block_Tracker extends Mage_Core_Block_Template {

    protected $_lastOrderId;
    protected $_lastOrder;
    protected $_store;

    protected function _construct()
    {
        $this->_lastOrderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
        $this->_lastOrder = Mage::getModel('sales/order')->load($this->_lastOrderId);
        $this->_store = Mage::app()->getStore();
    }

    public function getLastOrder()
    {
         return $this->_lastOrder;
    }

    public function getLastOrderId()
    {
         return $this->_lastOrderId;
    }

    public function getOrderItems()
    {
         return $this->_lastOrder->getAllItems();
    }

    public function getCurrencyCode()
    {
         return $this->_store->getCurrentCurrencyCode();
    }
}

Finally you can create your template file app/design/frontend/base/default/template/digitalpianism/pixeltracking/tracker.phtml

<?php

$lastOrderId = $this->getLastOrderId();
$lastOrder = $this->getLastOrder();
$orderItems = $this->getOrderItems();
$currency = $this->getCurrencyCode();

// Display your pixel tracker here

This template is probably the most custom part and you will have to add the specific code of your tracker in replacement of the comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.