├── app
├── etc
│ └── modules
│ │ └── Chapagain_GoogleTagManager.xml
├── code
│ └── community
│ │ └── Chapagain
│ │ └── GoogleTagManager
│ │ ├── Model
│ │ └── Gtm.php
│ │ ├── controllers
│ │ └── IndexController.php
│ │ ├── etc
│ │ ├── adminhtml.xml
│ │ ├── config.xml
│ │ └── system.xml
│ │ ├── Helper
│ │ └── Data.php
│ │ └── Block
│ │ ├── System
│ │ └── Config
│ │ │ └── Info.php
│ │ └── Gtm.php
└── design
│ └── frontend
│ └── base
│ └── default
│ ├── template
│ └── chapagain
│ │ └── googletagmanager
│ │ ├── noscript.phtml
│ │ └── gtm.phtml
│ └── layout
│ └── chapagain
│ └── googletagmanager.xml
└── README.md
/app/etc/modules/Chapagain_GoogleTagManager.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | true
6 | community
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/GoogleTagManager/Model/Gtm.php:
--------------------------------------------------------------------------------
1 | loadByAttribute('sku',$sku);
13 | return $collection;
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/app/design/frontend/base/default/template/chapagain/googletagmanager/noscript.phtml:
--------------------------------------------------------------------------------
1 | helper('googletagmanager');
3 | $_containerId = trim($_helper->getContainerId());
4 | if ($_helper->getIsEnable()):
5 | ?>
6 |
7 |
8 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/GoogleTagManager/controllers/IndexController.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Allow Everything
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 | GoogleTagManager
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/app/design/frontend/base/default/layout/chapagain/googletagmanager.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/design/frontend/base/default/template/chapagain/googletagmanager/gtm.phtml:
--------------------------------------------------------------------------------
1 | helper('googletagmanager');
3 | $_containerId = trim($_helper->getContainerId());
4 | if ($_helper->getIsEnable()):
5 | ?>
6 |
7 |
8 | getIsEnableDataLayer()):
10 | echo $this->getDataLayerProduct(); // Product Detail Page
11 | echo $this->getDataLayerOrder(); // Order Success Page
12 | endif;
13 | ?>
14 |
15 |
16 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/GoogleTagManager/Helper/Data.php:
--------------------------------------------------------------------------------
1 | getCustomer()->getId();
48 | if (!$customerId) {
49 | return false;
50 | }
51 | return $customerId;
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/GoogleTagManager/etc/config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 0.1.3
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | Chapagain_GoogleTagManager
14 | googletagmanager
15 |
16 |
17 |
18 |
19 |
20 |
21 | chapagain/googletagmanager.xml
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 | Chapagain_GoogleTagManager
32 | googletagmanageradmin
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 | Chapagain_GoogleTagManager_Model
41 |
42 |
43 |
44 |
45 | Chapagain_GoogleTagManager_Block
46 |
47 |
48 |
49 |
50 | Chapagain_GoogleTagManager_Helper
51 |
52 |
53 |
54 |
55 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/GoogleTagManager/Block/System/Config/Info.php:
--------------------------------------------------------------------------------
1 |
23 |
About the Developer
24 |
25 | Mukesh Chapagain
26 | Web developer, Programmer, Blogger, Engineer
27 | Freelancer with specialization in Magento eCommerce
28 |
29 | Blog |
30 | Email |
31 | LinkedIn |
32 | GitHub |
33 | Twitter |
34 | Facebook
35 |
36 | More extensions by the developer at Magento Marketplace
37 |
38 | ';
39 |
40 | return $html;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Google Tag Manager - Extension for Magento 1.x
2 |
3 | This extension allows you to integrate Google Tag Manager on your Magento site so that you can track
4 | and gather valuable visitor data from your website and make important decisions to grow your
5 | business. This extension also provides e-commerce transaction tracking feature. You just need to enter
6 | your Tag Manager container id in the extension's configuration settings.
7 |
8 | [Google Tag Manager](https://support.google.com/tagmanager/answer/6102821?hl=en) allows you to quickly and easily update tags and code snippets on your website or
9 | mobile app, such as those intended for traffic analysis and marketing optimization. You can add and
10 | update AdWords, Google Analytics, Floodlight, and 3rd party or custom tags from the Google Tag
11 | Manager user interface instead of editing site code. This reduces errors, frees you from having to
12 | involve a web developer, and allows you to quickly deploy new features or content onto your site.
13 |
14 | Google Tag Manager are FREE of charge services offered by Google. You need to create a separate
15 | Google Tag Manager account from [here](https://tagmanager.google.com/?hl=en#/admin/accounts/create) and link the account with your Magento store by adding the
16 | "Container ID" of your Google Tag Manager account to the current extension's configuration setting.
17 |
18 | ## Features
19 |
20 | - Tracks views of the product
21 | - Tracks categories and price of the product
22 | - Tracks transaction of the product by SKU, name, category, price and quantity
23 | - Tracks the transaction purchase revenue, tax and shipping cost
24 | - Tracks the coupon code
25 |
26 | ## Installation
27 |
28 | 1. The module's files should be placed in Magento root folder.
29 | 2. Login to your Magento site's admin
30 | 3. Disable Compilation if it is enabled: `System -> Tools -> Compilation`
31 | 4. Refresh Cache: `System -> Cache Management`
32 | 5. That's all. The extension should be installed by now.
33 | 6. You can re-enable Compilation if it was enabled before.
34 |
35 | ## Configuration Settings
36 |
37 | 1. Login to your Magento site's admin
38 | 2. Go to `System → Configuration` page
39 | 3. On left sidebar, click on `CHAPAGAIN EXTENSIONS → Google Tag Manager` menu
40 | 4. Here, you will see the following settings from where you can:
41 | - Enable/Disable module
42 | - Enable/Disable data layer
43 | - Add container ID of your site from Google Tag Manager
44 |
45 | ## Links
46 |
47 | - This extension is also available on Magento Marketplace: [Google Tag Manager on Magento Marketplace](https://marketplace.magento.com/chapagain-chapagain-googletagmanager.html)
48 | - BLOG: [http://blog.chapagain.com.np/google-tag-manager-gtm-magento-extension-free/](http://blog.chapagain.com.np/google-tag-manager-gtm-magento-extension-free/)
49 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/GoogleTagManager/etc/system.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | 250
7 |
8 |
9 |
10 |
11 |
12 | chapagainall
13 | text
14 | 10
15 | 1
16 | 1
17 | 1
18 |
19 |
20 | googletagmanager/system_config_info
21 | 10
22 | 1
23 | 1
24 | 1
25 |
26 |
27 |
28 |
29 |
30 |
31 | chapagainall
32 | text
33 | 100
34 | 1
35 | 1
36 | 1
37 |
38 |
39 |
40 | text
41 | 20
42 | 1
43 | 1
44 | 1
45 |
46 |
47 |
48 | Enable/Disable Google Tag Manager
49 | select
50 | adminhtml/system_config_source_yesno
51 | 10
52 | 1
53 | 1
54 | 1
55 |
56 |
57 |
58 | GTM Container ID
59 | text
60 | 20
61 | 1
62 | 1
63 | 1
64 |
65 |
66 |
67 |
68 | select
69 | adminhtml/system_config_source_yesno
70 | 30
71 | 1
72 | 1
73 | 1
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
--------------------------------------------------------------------------------
/app/code/community/Chapagain/GoogleTagManager/Block/Gtm.php:
--------------------------------------------------------------------------------
1 | getRequest()->getRouteName();
8 | $identifier = Mage::getSingleton('cms/page')->getIdentifier();
9 |
10 | if($routeName == 'cms' && $identifier == 'home') {
11 | return true;
12 | } else {
13 | return false;
14 | }
15 | }
16 |
17 | public function getIsOrderSuccessPage()
18 | {
19 | if (strpos(Mage::app()->getRequest()->getPathInfo(), '/checkout/onepage/success') !== false) {
20 | return true;
21 | }
22 | return false;
23 | }
24 |
25 | public function getIsCartPage()
26 | {
27 | if (strpos(Mage::app()->getRequest()->getPathInfo(), '/checkout/cart') !== false) {
28 | return true;
29 | }
30 | return false;
31 | }
32 |
33 | public function getIsCheckoutPage()
34 | {
35 | if (strpos(Mage::app()->getRequest()->getPathInfo(), '/checkout/onepage') !== false) {
36 | return true;
37 | }
38 | return false;
39 | }
40 |
41 | public function getCurrentProduct()
42 | {
43 | return Mage::registry('current_product');
44 | }
45 |
46 | public function getCurrentCategory()
47 | {
48 | return Mage::registry('current_category');
49 | }
50 |
51 | public function getOrder()
52 | {
53 | if ($this->getIsOrderSuccessPage()) {
54 | $orderId = Mage::getSingleton('checkout/session')->getLastOrderId();
55 | $order = Mage::getModel('sales/order')->load($orderId);
56 | if (!$order) {
57 | return false;
58 | }
59 | return $order;
60 | }
61 | return false;
62 | }
63 |
64 | public function getDataLayerProduct()
65 | {
66 | if ($product = $this->getCurrentProduct()) {
67 |
68 | $categoryCollection = $product->getCategoryCollection();
69 |
70 | $categories = array();
71 | foreach ($categoryCollection as $category) {
72 | $categories[] = Mage::getModel('catalog/category')->load($category->getEntityId())->getName();
73 | }
74 |
75 | $objProduct = new stdClass();
76 | $objProduct->name = $product->getName();
77 | $objProduct->id = $product->getSku();
78 | //$objProduct->price = Mage::getModel('directory/currency')->formatTxt($product->getPrice(), array('display' => Zend_Currency::NO_SYMBOL));
79 |
80 | //$objProduct->price = Mage::getModel('directory/currency')->formatTxt(Mage::helper('tax')->getPrice($product, $product->getFinalPrice(), true), array('display' => Zend_Currency::NO_SYMBOL));
81 |
82 | /**
83 | * Fix for comma separated price when price is 1000 or more
84 | * it becomes like 1,230 or 1,235.55
85 | * so, removing commas from price
86 | * which make the price 1230 or 1235.55
87 | */
88 | //$objProduct->price = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt(Mage::helper('tax')->getPrice($product, $product->getFinalPrice(), true), array('display' => Zend_Currency::NO_SYMBOL)));
89 |
90 | /**
91 | * Fix for another issue which came up after the above fix
92 | *
93 | * Fix for comma separated price for different locale currencies
94 | * System -> Configuration -> General -> Locale Options
95 | * This menu has the option to choose different countries locale.
96 | *
97 | * By default, it's the English (United States) and the above fix of removing commas from price works fine with English locale but there's problem with other locale like Dutch locale where price is comma separated instead of decimal separated.
98 | *
99 | * Price display of Dutch locale is the opposite of English locale
100 | * English locale price = 1,230.55 becomes the following in Dutch locale:
101 | * Dutch locale price = 1.230,55
102 | */
103 | $objProduct->price = (string) Mage::app()->getLocale()->getNumber(Mage::helper('tax')->getPrice($product, $product->getFinalPrice(), true));
104 |
105 | $objProduct->category = implode('|', $categories);
106 |
107 | $objEcommerce = new stdClass();
108 | $objEcommerce->ecommerce = new stdClass();
109 | $objEcommerce->ecommerce->detail = new stdClass();
110 | $objEcommerce->ecommerce->detail->actionField = new stdClass();
111 | $objEcommerce->ecommerce->detail->actionField->list = 'Catalog';
112 | $objEcommerce->ecommerce->detail->products = $objProduct;
113 |
114 | /*$objAddToCart = new stdClass();
115 | $objAddToCart->event = 'addToCart';
116 | $objAddToCart->ecommerce = new stdClass();
117 | $objAddToCart->ecommerce->add = new stdClass();
118 | $objAddToCart->ecommerce->add->products = $objProduct;*/
119 |
120 | $pageCategory = json_encode(array('pageCategory' => 'product-detail'), JSON_PRETTY_PRINT);
121 |
122 | $dataScript = PHP_EOL;
123 |
124 | $dataScript .= '';
125 |
126 | $dataScript .= PHP_EOL.PHP_EOL;
127 |
128 | $dataScript .= '';
129 |
130 | //$dataScript .= PHP_EOL.PHP_EOL;
131 |
132 | //$dataScript .= '';
133 |
134 | return $dataScript;
135 | }
136 | }
137 |
138 | public function getDataLayerOrder()
139 | {
140 | if ($order = $this->getOrder()) {
141 |
142 | $aItems = array();
143 | $productItems = array();
144 | foreach ($order->getAllVisibleItems() as $item) {
145 |
146 | $categoryCollection = $item->getProduct()->getCategoryCollection();
147 | $categories = array();
148 | foreach ($categoryCollection as $category) {
149 | $categories[] = Mage::getModel('catalog/category')->load($category->getEntityId())->getName();
150 | }
151 |
152 | $productItem = array();
153 | $productItem['name'] = $item->getName();
154 | $productItem['id'] = $item->getSku();
155 |
156 | //$productItem['price'] = Mage::getModel('directory/currency')->formatTxt($item->getBasePrice(), array('display' => Zend_Currency::NO_SYMBOL));
157 | //$productItem['price'] = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($item->getBasePrice(), array('display' => Zend_Currency::NO_SYMBOL)));
158 | $productItem['price'] = (string) Mage::app()->getLocale()->getNumber($item->getBasePrice());
159 |
160 | $productItem['category'] = implode('|', $categories);
161 | $productItem['quantity'] = intval($item->getQtyOrdered()); // converting qty from decimal to integer
162 | $productItem['coupon'] = '';
163 | $productItems[] = (object) $productItem;
164 |
165 | $objItem = array();
166 | $objItem['sku'] = $item->getSku();
167 | $objItem['name'] = $item->getName();
168 | $objItem['category'] = implode('|', $categories);
169 |
170 | //$objItem['price'] = Mage::getModel('directory/currency')->formatTxt($item->getBasePrice(), array('display' => Zend_Currency::NO_SYMBOL));
171 | //$objItem['price'] = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($item->getBasePrice(), array('display' => Zend_Currency::NO_SYMBOL)));
172 | $objItem['price'] = (string) Mage::app()->getLocale()->getNumber($item->getBasePrice());
173 |
174 | $objItem['quantity'] = intval($item->getQtyOrdered()); // converting qty from decimal to integer
175 | $aItems[] = (object) $objItem;
176 | }
177 |
178 | $objOrder = new stdClass();
179 |
180 | $objOrder->transactionId = $order->getIncrementId();
181 | $objOrder->transactionAffiliation = Mage::app()->getStore()->getFrontendName();
182 |
183 | //$objOrder->transactionTotal = Mage::getModel('directory/currency')->formatTxt($order->getBaseGrandTotal(), array('display' => Zend_Currency::NO_SYMBOL));
184 | //$objOrder->transactionTax = Mage::getModel('directory/currency')->formatTxt($order->getBaseTaxAmount(), array('display' => Zend_Currency::NO_SYMBOL));
185 | //$objOrder->transactionShipping = Mage::getModel('directory/currency')->formatTxt($order->getBaseShippingAmount(), array('display' => Zend_Currency::NO_SYMBOL));
186 |
187 | //$objOrder->transactionTotal = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($order->getBaseGrandTotal(), array('display' => Zend_Currency::NO_SYMBOL)));
188 | //$objOrder->transactionTax = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($order->getBaseTaxAmount(), array('display' => Zend_Currency::NO_SYMBOL)));
189 | //$objOrder->transactionShipping = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($order->getBaseShippingAmount(), array('display' => Zend_Currency::NO_SYMBOL)));
190 |
191 | $objOrder->transactionTotal = (float) Mage::app()->getLocale()->getNumber($order->getBaseGrandTotal());
192 | $objOrder->transactionTax = (float) Mage::app()->getLocale()->getNumber($order->getBaseTaxAmount());
193 | $objOrder->transactionShipping = (float) Mage::app()->getLocale()->getNumber($order->getBaseShippingAmount());
194 |
195 |
196 | $objOrder->transactionProducts = $aItems;
197 |
198 | $objOrder->ecommerce = new stdClass();
199 | $objOrder->ecommerce->purchase = new stdClass();
200 | $objOrder->ecommerce->purchase->actionField = new stdClass();
201 | $objOrder->ecommerce->purchase->actionField->id = $order->getIncrementId();
202 | $objOrder->ecommerce->purchase->actionField->affiliation = Mage::app()->getStore()->getFrontendName();
203 |
204 | //$objOrder->ecommerce->purchase->actionField->revenue = Mage::getModel('directory/currency')->formatTxt($order->getBaseGrandTotal(), array('display' => Zend_Currency::NO_SYMBOL));
205 | //$objOrder->ecommerce->purchase->actionField->tax = Mage::getModel('directory/currency')->formatTxt($order->getBaseTaxAmount(), array('display' => Zend_Currency::NO_SYMBOL));
206 | //$objOrder->ecommerce->purchase->actionField->shipping = Mage::getModel('directory/currency')->formatTxt($order->getBaseShippingAmount(), array('display' => Zend_Currency::NO_SYMBOL));
207 |
208 | //$objOrder->ecommerce->purchase->actionField->revenue = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($order->getBaseGrandTotal(), array('display' => Zend_Currency::NO_SYMBOL)));
209 | //$objOrder->ecommerce->purchase->actionField->tax = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($order->getBaseTaxAmount(), array('display' => Zend_Currency::NO_SYMBOL)));
210 | //$objOrder->ecommerce->purchase->actionField->shipping = str_replace(',', '', Mage::getModel('directory/currency')->formatTxt($order->getBaseShippingAmount(), array('display' => Zend_Currency::NO_SYMBOL)));
211 |
212 | $objOrder->ecommerce->purchase->actionField->revenue = (float) Mage::app()->getLocale()->getNumber($order->getBaseGrandTotal());
213 | $objOrder->ecommerce->purchase->actionField->tax = (float) Mage::app()->getLocale()->getNumber($order->getBaseTaxAmount());
214 | $objOrder->ecommerce->purchase->actionField->shipping = (float) Mage::app()->getLocale()->getNumber($order->getBaseShippingAmount());
215 |
216 |
217 | $coupon = $order->getCouponCode();
218 | $objOrder->ecommerce->purchase->actionField->coupon = $coupon == null ? '' : $coupon;
219 |
220 | $objOrder->ecommerce->products = $productItems;
221 |
222 | $pageCategory = json_encode(array('pageCategory' => 'order-success'), JSON_PRETTY_PRINT);
223 |
224 | $dataScript = PHP_EOL;
225 |
226 | $dataScript .= '';
227 |
228 | $dataScript .= PHP_EOL.PHP_EOL;
229 |
230 | $dataScript .= '';
231 |
232 | return $dataScript;
233 | }
234 | }
235 | }
236 |
--------------------------------------------------------------------------------