├── Block
├── DataLayer.php
└── Gtm.php
├── Helper
└── Data.php
├── README.md
├── composer.json
├── etc
├── adminhtml
│ └── system.xml
└── module.xml
├── registration.php
└── view
└── frontend
├── layout
├── catalog_category_view.xml
├── catalog_product_view.xml
├── customer_account_login.xml
└── default.xml
└── templates
├── data_layer.phtml
└── gtm.phtml
/Block/DataLayer.php:
--------------------------------------------------------------------------------
1 |
6 | * @license http://opensource.org/licenses/MIT
7 | */
8 |
9 | namespace Google\TagManager\Block;
10 |
11 | use Magento\Framework\View\Element\Template;
12 | use Magento\Framework\View\Element\Template\Context;
13 | use Magento\Cookie\Helper\Cookie as CookieHelper;
14 | use Google\TagManager\Helper\Data as GtmHelper;
15 |
16 | /**
17 | * Google Tag Manager Page Block
18 | */
19 | class DataLayer extends Template
20 | {
21 | /**
22 | * Data Layer Variables
23 | *
24 | * @var array;
25 | */
26 | protected $_variables = array();
27 |
28 | /**
29 | * Google Tag Manager data
30 | *
31 | * @var \Google\TagManager\Helper\Data
32 | */
33 | protected $_gtmHelper = null;
34 |
35 | /**
36 | * Cookie Helper
37 | *
38 | * @var \Magento\Cookie\Helper\Cookie
39 | */
40 | protected $_cookieHelper = null;
41 |
42 | /**
43 | * @param Context $context
44 | * @param CookieHelper $cookieHelper
45 | * @param GtmHelper $gtmHelper
46 | * @param array $data
47 | */
48 | public function __construct(
49 | Context $context,
50 | GtmHelper $gtmHelper,
51 | CookieHelper $cookieHelper,
52 | array $data = []
53 | ) {
54 | $this->_cookieHelper= $cookieHelper;
55 | $this->_gtmHelper = $gtmHelper;
56 | parent::__construct($context, $data);
57 | $this->_variables['ecommerce']['currencyCode'] = $this->_storeManager->getStore()->getCurrentCurrency()->getCode();
58 | }
59 |
60 | /**
61 | * Render tag manager script
62 | *
63 | * @return string
64 | */
65 | protected function _toHtml()
66 | {
67 | if ($this->_cookieHelper->isUserNotAllowSaveCookie() || !$this->_gtmHelper->isEnabled()) {
68 | return '';
69 | }
70 |
71 | return parent::_toHtml();
72 | }
73 |
74 | /**
75 | * Return Data Layer Variables
76 | *
77 | * @return array
78 | */
79 | public function getVariables()
80 | {
81 | return $this->_variables;
82 | }
83 |
84 | public function addVariable($name, $value)
85 | {
86 | if (!empty($name) && !empty($value)) {
87 | $this->_variables[$name] = $value;
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Block/Gtm.php:
--------------------------------------------------------------------------------
1 |
6 | * @license http://opensource.org/licenses/MIT
7 | */
8 |
9 | namespace Google\TagManager\Block;
10 |
11 | use Magento\Framework\View\Element\Template;
12 | use Magento\Framework\View\Element\Template\Context;
13 | use Magento\Cookie\Helper\Cookie as CookieHelper;
14 | use Google\TagManager\Helper\Data as GtmHelper;
15 |
16 | /**
17 | * Google Tag Manager Page Block
18 | */
19 | class Gtm extends Template
20 | {
21 | /**
22 | * Google Tag Manager data
23 | *
24 | * @var \Google\TagManager\Helper\Data
25 | */
26 | protected $_gtmHelper = null;
27 |
28 | /**
29 | * Cookie Helper
30 | *
31 | * @var \Magento\Cookie\Helper\Cookie
32 | */
33 | protected $_cookieHelper = null;
34 |
35 | /**
36 | * @param Context $context
37 | * @param CookieHelper $cookieHelper
38 | * @param GtmHelper $gtmHelper
39 | * @param array $data
40 | */
41 | public function __construct(
42 | Context $context,
43 | CookieHelper $cookieHelper,
44 | GtmHelper $gtmHelper,
45 | array $data = []
46 | ) {
47 | $this->_cookieHelper = $cookieHelper;
48 | $this->_gtmHelper = $gtmHelper;
49 | parent::__construct($context, $data);
50 | }
51 |
52 | /**
53 | * Get Account Id
54 | *
55 | * @return mixed
56 | */
57 | public function getAccountId()
58 | {
59 | return $this->_gtmHelper->getAccountId();
60 | }
61 |
62 | /**
63 | * Render tag manager script
64 | *
65 | * @return string
66 | */
67 | protected function _toHtml()
68 | {
69 | if ($this->_cookieHelper->isUserNotAllowSaveCookie() || !$this->_gtmHelper->isEnabled()) {
70 | return '';
71 | }
72 |
73 | return parent::_toHtml();
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Helper/Data.php:
--------------------------------------------------------------------------------
1 |
6 | * @license http://opensource.org/licenses/MIT
7 | */
8 |
9 | namespace Google\TagManager\Helper;
10 |
11 | use Magento\Store\Model\Store;
12 |
13 | /**
14 | * Google Tag Manager data helper
15 | *
16 | */
17 | class Data extends \Magento\Framework\App\Helper\AbstractHelper
18 | {
19 | /**
20 | * Config paths for using throughout the code
21 | */
22 | const XML_PATH_ACTIVE = 'google/tagmanager/active';
23 |
24 | const XML_PATH_ACCOUNT = 'google/tagmanager/account';
25 |
26 | /**
27 | * Whether Tag Manager is ready to use
28 | *
29 | * @return bool
30 | */
31 | public function isEnabled()
32 | {
33 | $accountId = $this->scopeConfig->getValue(self::XML_PATH_ACCOUNT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
34 | return $accountId && $this->scopeConfig->isSetFlag(self::XML_PATH_ACTIVE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
35 | }
36 |
37 | /**
38 | * Get Tag Manager Account ID
39 | *
40 | * @return bool | null | string
41 | */
42 | public function getAccountId()
43 | {
44 | return $this->scopeConfig->getValue(self::XML_PATH_ACCOUNT, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Google Tag Manager (Magento 2 Module)
2 |
3 | Magento 2 module for google tag manager support.
4 |
5 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "narayanvarma/magento2-google-tagmanager",
3 | "description": "Google - Tag Manager",
4 | "type": "magento2-module",
5 | "license": [
6 | "OSL-3.0",
7 | "AFL-3.0"
8 | ],
9 | "require": {
10 | "php": "~5.5.0|~5.6.0|~7.0.0",
11 | "magento/framework": "100.0.*"
12 | },
13 | "autoload": {
14 | "files": [
15 | "registration.php"
16 | ],
17 | "psr-4": {
18 | "Google\\TagManager\\": ""
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/etc/adminhtml/system.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 | Magento\Config\Model\Config\Source\Yesno
18 |
19 |
20 |
21 |
22 | 1
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/etc/module.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/registration.php:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 | pageCategory
13 | catalog-listing
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/view/frontend/layout/catalog_product_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
11 |
12 | pageCategory
13 | product-details
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/view/frontend/layout/customer_account_login.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 | pageCategory
15 | customer-login
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/view/frontend/layout/default.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/view/frontend/templates/data_layer.phtml:
--------------------------------------------------------------------------------
1 |
6 | * @license http://opensource.org/licenses/MIT
7 | */
8 | ?>
9 |
10 | getVariables(); ?>
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/view/frontend/templates/gtm.phtml:
--------------------------------------------------------------------------------
1 |
6 | * @license http://opensource.org/licenses/MIT
7 | */
8 | ?>
9 |
10 |
11 |
13 |
18 |
19 |
--------------------------------------------------------------------------------