Przeglądaj źródła

#886 Google Tag Manager module

Alex Trofimov 9 lat temu
rodzic
commit
81921af805

+ 46 - 0
modules/boonex/google_tagmanager/classes/BxGoogleTagManAlertsResponse.php

@@ -0,0 +1,46 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxGoogleTagManAlertsResponse extends BxDolAlertsResponse
+{
+    protected $_sModule;
+    protected $_oModule;
+
+    public function __construct()
+    {
+        parent::__construct();
+
+        $this->_sModule = 'bx_googletagman';
+        $this->_oModule = BxDolModule::getInstance($this->_sModule);
+    }
+
+    public function response($oAlert)
+    {
+    	$sMethod = 'process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
+    	if(method_exists($this, $sMethod))
+    		$this->$sMethod($oAlert);
+
+        parent::response($oAlert);
+    }
+
+    protected function processBxPaymentFinalizeCheckout(&$oAlert)
+    {
+        $aPending = $oAlert->aExtras['pending'];
+
+        $aItems = explode(':', $aPending['items']);
+        if(!$aItems)
+            return;
+        
+        $oAlert->aExtras['message'] = _t($oAlert->aExtras['message']) . $this->_oModule->serviceTrackingCodePurchase($aPending, $aItems);
+    }
+}
+
+/** @} */

+ 46 - 0
modules/boonex/google_tagmanager/classes/BxGoogleTagManConfig.php

@@ -0,0 +1,46 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxGoogleTagManConfig extends BxDolModuleConfig
+{
+    protected $_iSellerId;
+    protected $_iModuleId;
+    protected $_aTrackableProducts;
+
+    public function __construct($aModule)
+    {
+        parent::__construct($aModule);
+        
+        $this->_iSellerId = 38; //--- UNA Team
+        $this->_iModuleId = 20; //--- Market
+
+        $this->_aTrackableProducts = array();
+        $this->_aTrackableProducts[] = 57; //--- Professional (monthly)
+        $this->_aTrackableProducts[] = 59; //--- Professional (yearly)
+    }
+
+    public function getSellerId()
+    {
+        return $this->_iSellerId;
+    }
+
+    public function getModuleId()
+    {
+        return $this->_iModuleId;
+    }
+
+    public function getTrackableProducts()
+    {
+        return $this->_aTrackableProducts;
+    }
+}
+
+/** @} */

+ 20 - 0
modules/boonex/google_tagmanager/classes/BxGoogleTagManDb.php

@@ -0,0 +1,20 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxGoogleTagManDb extends BxDolModuleDb
+{
+    function __construct(&$oConfig)
+    {
+        parent::__construct($oConfig);
+    }
+}
+
+/** @} */

+ 119 - 0
modules/boonex/google_tagmanager/classes/BxGoogleTagManModule.php

@@ -0,0 +1,119 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxGoogleTagManModule extends BxDolModule
+{
+    public function __construct(&$aModule)
+    {
+        parent::__construct($aModule);        
+    }
+
+    public function serviceInjection($sName)
+    {
+        if (!($sGtmContainerId = getParam('bx_googletagman_container_id')))
+            return '';
+
+        switch ($sName) {
+        case 'injection_head_begin':
+
+            $oProfile = BxDolProfile::getInstance();
+            $oAccount = $oProfile ? $oProfile->getAccountObject() : null;
+            $aProfileInfo = $oProfile ? $oProfile->getInfo() : array('type' => 'guest', 'status' => 'active');
+            $aMembership = BxDolAcl::getInstance()->getMemberMembershipInfo($oProfile ? $oProfile->id() : 0);
+            $aKeySecrets = $oProfile ? BxDolService::call('bx_oauth', 'get_clients_by', array(array('type' => 'user_id', 'user_id' => $oProfile->id()))) : array();
+
+            $aDataLayer = array(
+                'membership-id' => $aMembership['id'],
+                'membership-name' => str_replace('_adm_prm_txt_level_', '', $aMembership['name']),
+                'profile-type' => $aProfileInfo['type'],
+                'profile-status' => $aProfileInfo['status'],                
+                'account-email-confirmed' => (int)($oAccount ? $oAccount->isConfirmed() : 0),
+                'account-profiles-count' => $oAccount ? $oAccount->getProfilesNumber() : 0,
+                'keys-secrets-count' => count($aKeySecrets),
+            );
+            $sDataLayer = json_encode($aDataLayer);
+
+            return <<<EOF
+<script>
+  dataLayer = [$sDataLayer];
+</script>
+<!-- Google Tag Manager -->
+<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=
+'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
+})(window,document,'script','dataLayer','$sGtmContainerId');</script>
+<!-- End Google Tag Manager -->
+EOF;
+        case 'injection_header':
+            
+            return <<<EOF
+<!-- Google Tag Manager (noscript) -->
+<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=$sGtmContainerId"
+height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
+<!-- End Google Tag Manager (noscript) -->
+EOF;
+        }
+    }
+
+    public function serviceTrackingCodeRegister()
+    {
+        if (!bx_get('register'))
+            return '';
+
+        return $this->getTrackingCode (array(
+            'event' => 'register',
+        ));
+    }
+    
+    public function serviceTrackingCodePurchase($aTransaction, $aProducts)
+    {
+        $a = array(
+            'event' => 'purchase',
+            'ecommerce' => array(
+                'currencyCode' => BxDolPayments::getInstance()->getOption('default_currency_code'),
+                'purchase' => array (
+                    'actionField' => array (
+                        'id' => $aTransaction['order'],
+                        'revenue'=> number_format((float)$aTransaction['amount'], 2, '.', ''),
+                    ),
+                    'products' => array()
+                ),
+            ),
+        );
+
+        foreach ($aProducts as $sProduct) {
+            $aProduct = explode('_', $sProduct);
+            $a['ecommerce']['purchase']['products'][] = array(
+                'id' => $aProduct[2],
+                'brand' => BxDolProfile::getInstance($aProduct[0])->getDisplayName(),
+
+                'vendor-profile-id' => $aProduct[0],
+                'module-id' => $aProduct[1],                
+                'quantity' => $aProduct[3],
+            );
+        }
+        
+        return $this->getTrackingCode ($a);
+    }
+
+    protected function getTrackingCode ($aParams)
+    {
+        $sParams = json_encode($aParams);
+        return <<<EOF
+<script type="text/javascript">
+    dataLayer.push($sParams);
+</script>
+EOF;
+    }
+}
+
+/** @} */

+ 20 - 0
modules/boonex/google_tagmanager/classes/BxGoogleTagManTemplate.php

@@ -0,0 +1,20 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxGoogleTagManTemplate extends BxDolModuleTemplate
+{
+    function __construct(&$oConfig, &$oDb)
+    {
+        parent::__construct($oConfig, $oDb);
+    }
+}
+
+/** @} */

+ 68 - 0
modules/boonex/google_tagmanager/install/config.php

@@ -0,0 +1,68 @@
+<?php
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+$aConfig = array(
+    /**
+     * Main Section.
+     */
+    'type' => BX_DOL_MODULE_TYPE_MODULE,
+    'name' => 'bx_googletagman',
+    'title' => 'Google Tag Manager',
+    'note' => 'This module adds Google Tag Manager integration',
+    'version' => '9.0.0',
+    'vendor' => 'BoonEx',
+	'help_url' => 'http://feed.boonex.com/?section={module_name}',
+
+    'compatible_with' => array(
+        '9.0.x'
+    ),
+
+    /**
+     * 'home_dir' and 'home_uri' - should be unique. Don't use spaces in 'home_uri' and the other special chars.
+     */
+    'home_dir' => 'boonex/google_tagmanager/',
+    'home_uri' => 'google_tagmanager',
+
+    'db_prefix' => 'bx_googletagman_',
+    'class_prefix' => 'BxGoogleTagMan',
+
+    /**
+     * Category for language keys.
+     */
+    'language_category' => 'Google Tag Manager',
+
+    /**
+     * Installation/Uninstallation Section.
+     */
+    'install' => array(
+        'execute_sql' => 1,
+        'update_languages' => 1,
+    ),
+    'uninstall' => array (
+        'execute_sql' => 1,
+        'update_languages' => 1,
+    ),
+    'enable' => array(
+        'execute_sql' => 1,
+        'clear_db_cache' => 1,
+    ),
+    'disable' => array(
+        'execute_sql' => 1,
+        'clear_db_cache' => 1,
+    ),
+
+    /**
+     * Dependencies Section
+     */
+    'dependencies' => array(),
+);
+
+/** @} */

+ 20 - 0
modules/boonex/google_tagmanager/install/installer.php

@@ -0,0 +1,20 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+class BxGoogleTagManInstaller extends BxDolStudioInstaller
+{
+    function __construct($aConfig)
+    {
+        parent::__construct($aConfig);
+    }
+}
+
+/** @} */

+ 5 - 0
modules/boonex/google_tagmanager/install/langs/en.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources name="en" flag="gb" title="English">
+	<string name="_bx_googletagman"><![CDATA[Google Tag Manager]]></string>
+	<string name="_bx_googletagman_option_container_id"><![CDATA[Google Tag Manager Container ID]]></string>
+</resources>

+ 17 - 0
modules/boonex/google_tagmanager/install/sql/disable.sql

@@ -0,0 +1,17 @@
+
+-- OPTIONS
+DELETE FROM `top`, `toc`, `to` USING `sys_options_types` AS `top` LEFT JOIN `sys_options_categories` AS `toc` ON `top`.`id`=`toc`.`type_id` LEFT JOIN `sys_options` AS `to` ON `toc`.`id`=`to`.`category_id` WHERE `top`.`name` = 'bx_googletagman';
+
+
+-- INJECTIONS
+DELETE FROM `sys_injections` WHERE `name` IN('bx_googletagman_track_js', 'bx_googletagman_track_no_js');
+
+
+-- PAGES
+DELETE FROM `sys_pages_blocks` WHERE `module` = 'bx_googletagman';
+
+
+-- ALERTS
+SET @iHandler := (SELECT `id` FROM `sys_alerts_handlers` WHERE `name` = 'bx_googletagman' LIMIT 1);
+DELETE FROM `sys_alerts` WHERE `handler_id` = @iHandler;
+DELETE FROM `sys_alerts_handlers` WHERE `id` = @iHandler;

+ 33 - 0
modules/boonex/google_tagmanager/install/sql/enable.sql

@@ -0,0 +1,33 @@
+
+-- OPTIONS
+SET @iTypeOrder = (SELECT MAX(`order`) FROM `sys_options_types` WHERE `group` = 'modules');
+INSERT INTO `sys_options_types` (`group`, `name`, `caption`, `icon`, `order`) VALUES 
+('modules', 'bx_googletagman', '_bx_unacon_adm_stg_cpt_type', 'bx_googletagman@modules/boonex/google_tagmanager/|std-icon.svg', IF(NOT ISNULL(@iTypeOrder), @iTypeOrder + 1, 1));
+SET @iTypeId = LAST_INSERT_ID();
+
+INSERT INTO `sys_options_categories` (`type_id`, `name`, `caption`, `order` )  
+VALUES (@iTypeId, 'bx_googletagman_general', '_sys_connect_adm_stg_cpt_category_general', 1);
+SET @iCategId = LAST_INSERT_ID();
+
+INSERT INTO `sys_options` (`name`, `value`, `category_id`, `caption`, `type`, `check`, `check_error`, `order`, `extra`) VALUES
+('bx_googletagman_container_id', '', @iCategId, '_bx_googletagman_option_container_id', 'digit', '', '', 10, '');
+
+
+-- INJECTIONS
+INSERT INTO `sys_injections` (`name`, `page_index`, `key`, `type`, `data`, `replace`, `active`) VALUES
+('bx_googletagman_track_js', 0, 'injection_head_begin', 'service', 'a:3:{s:6:"module";s:15:"bx_googletagman";s:6:"method";s:9:"injection";s:6:"params";a:1:{i:0;s:20:"injection_head_begin";}}', 0, 1),
+('bx_googletagman_track_no_js', 0, 'injection_header', 'service', 'a:3:{s:6:"module";s:15:"bx_googletagman";s:6:"method";s:9:"injection";s:6:"params";a:1:{i:0;s:16:"injection_header";}}', 0, 1);
+
+
+-- PAGES
+INSERT INTO `sys_pages_blocks` (`object`, `cell_id`, `module`, `title_system`, `title`, `designbox_id`, `visible_for_levels`, `type`, `content`, `deletable`, `copyable`, `active`, `order`) VALUES
+('sys_account_profile_switcher', 1, 'bx_googletagman', '', '_bx_googletagman', 0, 2147483647, 'service', 'a:2:{s:6:"module";s:15:"bx_googletagman";s:6:"method";s:22:"tracking_code_register";}', 0, 0, 1, 3);
+
+
+-- ALERTS
+INSERT INTO `sys_alerts_handlers` (`name`, `class`, `file`, `service_call`) VALUES 
+('bx_googletagman', 'BxGoogleTagManAlertsResponse', 'modules/boonex/google_tagmanager/classes/BxGoogleTagManAlertsResponse.php', '');
+SET @iHandler := LAST_INSERT_ID();
+
+INSERT INTO `sys_alerts` (`unit`, `action`, `handler_id`) VALUES
+('bx_payment', 'finalize_checkout', @iHandler);

+ 14 - 0
modules/boonex/google_tagmanager/install/sql/install.sql

@@ -0,0 +1,14 @@
+
+-- Studio page and widget
+
+INSERT INTO `sys_std_pages`(`index`, `name`, `header`, `caption`, `icon`) VALUES
+(3, 'bx_googletagman', '_bx_googletagman', '_bx_googletagman', 'bx_googletagman@modules/boonex/google_tagmanager/|std-icon.svg');
+SET @iPageId = LAST_INSERT_ID();
+
+SET @iParentPageId = (SELECT `id` FROM `sys_std_pages` WHERE `name` = 'home');
+SET @iParentPageOrder = (SELECT MAX(`order`) FROM `sys_std_pages_widgets` WHERE `page_id` = @iParentPageId);
+INSERT INTO `sys_std_widgets` (`page_id`, `module`, `url`, `click`, `icon`, `caption`, `cnt_notices`, `cnt_actions`) VALUES
+(@iPageId, 'bx_googletagman', '{url_studio}module.php?name=bx_googletagman', '', 'bx_googletagman@modules/boonex/google_tagmanager/|std-icon.svg', '_bx_googletagman', '', 'a:4:{s:6:"module";s:6:"system";s:6:"method";s:11:"get_actions";s:6:"params";a:0:{}s:5:"class";s:18:"TemplStudioModules";}');
+INSERT INTO `sys_std_pages_widgets` (`page_id`, `widget_id`, `order`) VALUES
+(@iParentPageId, LAST_INSERT_ID(), IF(ISNULL(@iParentPageOrder), 1, @iParentPageOrder + 1));
+

+ 8 - 0
modules/boonex/google_tagmanager/install/sql/uninstall.sql

@@ -0,0 +1,8 @@
+
+
+-- Studio page and widget
+
+DELETE FROM `tp`, `tw`, `tpw`
+USING `sys_std_pages` AS `tp`, `sys_std_widgets` AS `tw`, `sys_std_pages_widgets` AS `tpw`
+WHERE `tp`.`id` = `tw`.`page_id` AND `tw`.`id` = `tpw`.`widget_id` AND `tp`.`name` = 'bx_googletagman';
+

+ 16 - 0
modules/boonex/google_tagmanager/request.php

@@ -0,0 +1,16 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    GoogleTagMan Google Tag Manager
+ * @ingroup     UnaModules
+ *
+ * @{
+ */
+
+check_logged();
+
+BxDolRequest::processAsAction($GLOBALS['aModule'], $GLOBALS['aRequest']);
+
+/** @} */

+ 21 - 0
modules/boonex/google_tagmanager/template/images/icons/std-icon.svg

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<svg width="606px" height="606px" viewBox="0 0 606 606" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+    <defs>
+
+        <path d="M552.268958,552.268958 C600,504.537813 600,431.458333 600,300 C600,168.541667 600,95.4621875 552.268958,47.7310417 C504.537813,0 431.458333,0 300,0 C168.541667,0 95.4621875,0 47.7310417,47.7310417 C0,95.4621875 0,168.541667 0,300 C0,431.458333 0,504.537813 47.7310417,552.268958 C95.4621875,600 168.541667,600 300,600 C431.458333,600 504.537813,600 552.268958,552.268958 Z" id="path-2"></path>
+    </defs>
+    <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
+        <g id="una-connect" transform="translate(3.000000, 3.000000)">
+            <g id="Squircle2-Copy-6">
+                <g id="Shape">
+                    <use fill="#FFFFFF" xlink:href="#path-2"></use>
+                    <use stroke="#000000" stroke-width="4.04" fill="url(#linearGradient-1)" xlink:href="#path-2"></use>
+                </g>
+            </g>
+            <g clip-path="url(#b)" transform="scale(2.5, 2.5), translate(25.0, 22.0)">
+                <path fill="#4285F4" d="M180.5 87.5l-28-28-20-20L96 76l20 20-20 20-36.5 36.5 28 28c4.7 4.7 12.3 4.7 17 0l28-28 20-20 28-28c4.7-4.7 4.7-12.3 0-17z"/>            
+                <path fill="#4FC3F7" d="M132.5 39.5l-28-28c-4.7-4.7-12.3-4.7-17 0l-28 28-20 20-28 28c-4.7 4.7-4.7 12.3 0 17l28 28 20 20L96 116 76 96l20-20 36.5-36.5z"/>
+            </g>
+        </g>
+    </g>
+</svg>