Skip to content

Commit

Permalink
Additional enhanced analytics event tracking (#17)
Browse files Browse the repository at this point in the history
* Additional enhanced analytics event tracking
  • Loading branch information
nandadubey authored Nov 13, 2018
1 parent ecbeb90 commit bc3a75a
Show file tree
Hide file tree
Showing 19 changed files with 1,112 additions and 36 deletions.
646 changes: 646 additions & 0 deletions Block/Promotion/Pixel/Code.php

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions Block/Promotion/Pixel/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
/**
* Class Confirm
*
* @package Astound\Affirm\Block\Promotion
* @package Astound\Affirm\Block\Pixel
*/
class Confirm extends \Magento\Framework\View\Element\Template
{
Expand Down Expand Up @@ -142,7 +142,7 @@ public function getOrdersTrackingCode()
*/
protected function _toHtml()
{
if (!$this->affirmPixelHelper->isAffirmAnalyticsAvailable()) {
if (!$this->affirmPixelHelper->isCheckoutSuccessPixelEnabledConfig()) {
return '';
}

Expand All @@ -163,7 +163,6 @@ public function getOptions()
if ($config && isset($config['script']) && isset($config['apiKeyPublic'])) {
$options['script'] = $config['script'];
$options['public_api_key'] = $config['apiKeyPublic'];
$options['sessionId'] = ($this->getCustomerSessionId())? $this->getCustomerSessionId() : '';
}
}
return json_encode($options);
Expand Down
106 changes: 97 additions & 9 deletions Helper/Pixel.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,110 @@ public function __construct(
$this->affirmPaymentConfig = $configAffirm;
}

public function getDateMicrotime()
{
$microtime = explode(' ', microtime());
$msec = $microtime[0];
$msecArray = explode('.', $msec);
$date = date('Y-m-d-H-i-s') . '-' . $msecArray[1];
return $date;
}

/**
* Returns is pixel enabled
*
* @return bool
*/
public function isTrackPixelEnabledConfig()
{
return $this->affirmPaymentConfig->getPixelValue('enabled');
}

/**
* Get pixel active status for confirm page
* Returns is pixel placement for search query enabled
*
* @return bool
*/
public function isSearchTrackPixelEnabledConfig()
{
return $this->affirmPaymentConfig->getPixelValue('add_search');
}

/**
* Returns is pixel placement for product list page enabled
*
* @return bool
*/
public function isProductListTrackPixelEnabledConfig()
{
return $this->affirmPaymentConfig->getPixelValue('add_product_list');
}

/**
* Returns is pixel placement for product page enabled
*
* @return bool
*/
public function isProductViewTrackPixelEnabledConfig()
{
return $this->affirmPaymentConfig->getPixelValue('add_product_view');
}

/**
* Returns is pixel placement for add to cart action enabled
*
* @return bool
*/
public function isAddCartTrackPixelEnabledConfig()
{
return $this->affirmPaymentConfig->getPixelValue('add_cart');
}

/**
* Returns is pixel placement for checkout start action enabled
*
* @return bool
*/
public function isAddChkStartTrackPixelEnabledConfig()
{
return $this->affirmPaymentConfig->getPixelValue('add_checkout_start');
}


/**
* Returns is pixel placement for confirmation page enabled
*
* @return bool
*/
public function isCheckoutSuccessPixelEnabledConfig()
{
return $this->affirmPaymentConfig->getPixelValue('add_checkout_success');
}

/**
* Add slashes to string and prepares string for javascript.
*
* @param string $str
* @return string
*/
public function isAffirmAnalyticsAvailable()
public function escapeSingleQuotes($str)
{
return $this->affirmPaymentConfig->getPixelValue('active_confirm');
return str_replace("'", "\'", $str);
}

public function getDateMicrotime()
/**
* Based on provided configuration path returns configuration value.
*
* @param string $configPath
* @param string|int $scope
* @return string
*/
public function getConfig($configPath, $scope = 'default')
{
$microtime = explode(' ', microtime());
$msec = $microtime[0];
$msecArray = explode('.', $msec);
$date = date('Y-m-d-H-i-s') . '-' . $msecArray[1];
return $date;
return $this->scopeConfig->getValue(
$configPath,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$scope
);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "affirm/magento2",
"description": "Affirm's extension for the Magento 2 https://www.affirm.com/",
"type": "magento2-module",
"version": "2.1.0",
"version": "2.1.1",
"license": [
"BSD-3-Clause"
],
Expand Down
35 changes: 33 additions & 2 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,40 @@
</group>
<group id="affirm_pixel" translate="label" sortOrder="120" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Enhanced Analytics</label>
<field id="active_confirm" translate="label" type="select" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Enable pixel for confirmation page</label>
<field id="enabled" translate="label" type="select" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Enable Affirm pixel</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable affirm tracking pixels. In addition to enabling this config, please do enable pixel for specific actions using config options listed below.</comment>
</field>
<field id="add_cart" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Add to Cart</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable add to cart tracking</comment>
</field>
<field id="add_search" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Search</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable search tracking</comment>
</field>
<field id="add_product_view" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Product</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable product tracking</comment>
</field>
<field id="add_product_list" translate="label" type="select" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Product List</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable product list tracking</comment>
</field>
<field id="add_checkout_start" translate="label" type="select" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Checkout Start</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable checkout start tracking</comment>
</field>
<field id="add_checkout_success" translate="label" type="select" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Checkout Success</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Enable checkout success tracking</comment>
</field>
</group>
</section>
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Astound_Affirm" setup_version="2.1.0">
<module name="Astound_Affirm" setup_version="2.1.1">
<sequence>
<module name="Magento_Checkout"/>
<module name="Magento_Sales"/>
Expand Down
8 changes: 8 additions & 0 deletions view/frontend/layout/catalog_category_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,13 @@
</arguments>
</block>
</referenceContainer>
<referenceContainer name="before.body.end">
<block class="Astound\Affirm\Block\Promotion\Pixel\Code"
name="affirm_pixel_code_category"
template="Astound_Affirm::pixel/code.phtml"
as="affirm_pixel_javascript_code"
after="-"
ifconfig="affirm/affirm_pixel/enabled" />
</referenceContainer>
</body>
</page>
8 changes: 8 additions & 0 deletions view/frontend/layout/catalog_product_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,13 @@
</arguments>
</block>
</referenceContainer>
<referenceContainer name="before.body.end">
<block class="Astound\Affirm\Block\Promotion\Pixel\Code"
name="affirm_pixel_code_product"
template="Astound_Affirm::pixel/code.phtml"
as="affirm_pixel_javascript_code"
after="-"
ifconfig="affirm/affirm_pixel/enabled" />
</referenceContainer>
</body>
</page>
7 changes: 7 additions & 0 deletions view/frontend/layout/catalog_product_view_type_bundle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,12 @@
</arguments>
</block>
</referenceBlock>
<referenceContainer name="before.body.end">
<block class="Astound\Affirm\Block\Promotion\Pixel\Code"
name="affirm_pixel_code_product_bundle"
template="Astound_Affirm::pixel/code.phtml"
after="-"
ifconfig="affirm/affirm_pixel/enabled" />
</referenceContainer>
</body>
</page>
45 changes: 45 additions & 0 deletions view/frontend/layout/catalogsearch_advanced_result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<!--
~ /*
~ * BSD 3-Clause License
~ *
~ * Copyright (c) 2018, Affirm
~ * All rights reserved.
~ *
~ * Redistribution and use in source and binary forms, with or without
~ * modification, are permitted provided that the following conditions are met:
~ *
~ * Redistributions of source code must retain the above copyright notice, this
~ * list of conditions and the following disclaimer.
~ *
~ * Redistributions in binary form must reproduce the above copyright notice,
~ * this list of conditions and the following disclaimer in the documentation
~ * and/or other materials provided with the distribution.
~ *
~ * Neither the name of the copyright holder nor the names of its
~ * contributors may be used to endorse or promote products derived from
~ * this software without specific prior written permission.
~ *
~ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
~ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
~ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
~ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
~ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
~ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
~ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
~ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
~ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
~ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
~ */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
<block class="Astound\Affirm\Block\Promotion\Pixel\Code"
name="affirm_pixel_code_catalogsearch"
template="Astound_Affirm::pixel/code.phtml"
after="-"
ifconfig="affirm/affirm_pixel/enabled" />
</referenceContainer>
</body>
</page>
45 changes: 45 additions & 0 deletions view/frontend/layout/catalogsearch_result_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0"?>
<!--
~ /*
~ * BSD 3-Clause License
~ *
~ * Copyright (c) 2018, Affirm
~ * All rights reserved.
~ *
~ * Redistribution and use in source and binary forms, with or without
~ * modification, are permitted provided that the following conditions are met:
~ *
~ * Redistributions of source code must retain the above copyright notice, this
~ * list of conditions and the following disclaimer.
~ *
~ * Redistributions in binary form must reproduce the above copyright notice,
~ * this list of conditions and the following disclaimer in the documentation
~ * and/or other materials provided with the distribution.
~ *
~ * Neither the name of the copyright holder nor the names of its
~ * contributors may be used to endorse or promote products derived from
~ * this software without specific prior written permission.
~ *
~ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
~ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
~ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
~ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
~ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
~ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
~ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
~ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
~ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
~ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
~ */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="before.body.end">
<block class="Astound\Affirm\Block\Promotion\Pixel\Code"
name="affirm_pixel_code_catalog_search"
template="Astound_Affirm::pixel/code.phtml"
after="-"
ifconfig="affirm/affirm_pixel/enabled" />
</referenceContainer>
</body>
</page>
7 changes: 7 additions & 0 deletions view/frontend/layout/checkout_cart_index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@
</arguments>
</block>
</referenceContainer>
<referenceContainer name="before.body.end">
<block class="Astound\Affirm\Block\Promotion\Pixel\Code"
name="affirm_pixel_code_category"
template="Astound_Affirm::pixel/code.phtml"
after="-"
ifconfig="affirm/affirm_pixel/enabled" />
</referenceContainer>
</body>
</page>
9 changes: 7 additions & 2 deletions view/frontend/layout/checkout_onepage_success.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="order.success.additional.info">
<block class="Astound\Affirm\Block\Promotion\Pixel\Confirm" name="affirm_pixel_javascript" as="affirm_pixel_javascript" template="Astound_Affirm::pixel/confirm.phtml"/>
<referenceContainer name="before.body.end">
<block class="Astound\Affirm\Block\Promotion\Pixel\Confirm"
name="affirm_pixel_code_confirm"
template="Astound_Affirm::pixel/confirm.phtml"
as="affirm_pixel_javascript_code"
after="-"
ifconfig="affirm/affirm_pixel/enabled" />
</referenceContainer>
</body>
</page>
4 changes: 2 additions & 2 deletions view/frontend/requirejs-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ var config = {
map: {
'*': {
'synchPost': 'Astound_Affirm/js/action/send-request',
'affirmPixel': 'Astound_Affirm/js/affirmPixel',
'affirmWidget': 'Astound_Affirm/js/affirmWidget',
'aslowasPDP': 'Astound_Affirm/js/aslowasPDP',
'aslowasPLP': 'Astound_Affirm/js/aslowasPLP',
'aslowasCC': 'Astound_Affirm/js/aslowasCC',
'aslowasMC': 'Astound_Affirm/js/aslowasMC',
'affirmPixel': 'Astound_Affirm/js/affirmPixel'
'aslowasMC': 'Astound_Affirm/js/aslowasMC'
}
}
};
Loading

0 comments on commit bc3a75a

Please sign in to comment.