Skip to content
This repository was archived by the owner on Feb 3, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions ganalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function wrapOrder($id_order)
*/
public function hookOrderConfirmation($params)
{
$order = $params['objOrder'];
$order = $params['objOrder']; /** @var Order $order */
if (Validate::isLoadedObject($order) && $order->getCurrentState() != (int)Configuration::get('PS_OS_ERROR'))
{
$ga_order_sent = Db::getInstance()->getValue('SELECT id_order FROM `'._DB_PREFIX_.'ganalytics` WHERE id_order = '.(int)$order->id);
Expand All @@ -311,12 +311,19 @@ public function hookOrderConfirmation($params)
Db::getInstance()->Execute('INSERT INTO `'._DB_PREFIX_.'ganalytics` (id_order, id_shop, sent, date_add) VALUES ('.(int)$order->id.', '.(int)$this->context->shop->id.', 0, NOW())');
if ($order->id_customer == $this->context->cookie->id_customer)
{
$ga_scripts = '';

if ((int)$order->id_carrier > 0) { // Not a order with virtual products only
$carrierName = Db::getInstance()->getValue('SELECT name FROM ' . _DB_PREFIX_ . 'carrier WHERE id_carrier = ' . (int)$order->id_carrier);
$ga_scripts .= 'MBG.addCheckoutOption(2, \'' . $carrierName . '\');' . PHP_EOL;
}

$order_products = array();
$cart = new Cart($order->id_cart);
foreach ($cart->getProducts() as $order_product)
$order_products[] = $this->wrapProduct($order_product, array(), 0, true);

$ga_scripts = 'MBG.addCheckoutOption(3,\''.$order->payment.'\');';
$ga_scripts .= 'MBG.addCheckoutOption(3, \''.$order->payment.'\');' . PHP_EOL;

$transaction = array(
'id' => $order->id,
Expand Down Expand Up @@ -347,17 +354,17 @@ public function hookFooter()
{
$this->filterable = 0;

$gacarts = unserialize($this->context->cookie->ga_cart);
foreach ($gacarts as $gacart)
{
if ($gacart['quantity'] > 0)
$ga_scripts .= 'MBG.addToCart('.Tools::jsonEncode($gacart).');';
elseif ($gacart['quantity'] < 0)
{
$gacart['quantity'] = abs($gacart['quantity']);
$ga_scripts .= 'MBG.removeFromCart('.Tools::jsonEncode($gacart).');';
}
}
$gacarts = json_decode($this->context->cookie->ga_cart, true);
if (is_array($gacarts)) {
foreach ($gacarts as $gacart) {
if ($gacart['quantity'] > 0) {
$ga_scripts .= 'MBG.addToCart(' . Tools::jsonEncode($gacart) . ');';
} elseif ($gacart['quantity'] < 0) {
$gacart['quantity'] = abs($gacart['quantity']);
$ga_scripts .= 'MBG.removeFromCart(' . Tools::jsonEncode($gacart) . ');';
}
}
}
unset($this->context->cookie->ga_cart);
}

Expand Down Expand Up @@ -649,7 +656,12 @@ protected function _runJs($js_code, $backoffice = 0)
if (Configuration::get('GA_ACCOUNT_ID'))
{
$runjs_code = '';
if (!empty($js_code))

if (!empty($js_code)){
if($backoffice){
$js_code .= 'MBG.setCampaign(\'backoffice-orders\',\'backoffice\',\'cms\');';
}

$runjs_code .= '
<script type="text/javascript">
jQuery(document).ready(function(){
Expand All @@ -658,6 +670,7 @@ protected function _runJs($js_code, $backoffice = 0)
'.$js_code.'
});
</script>';
}

if (($this->js_state) != 1 && ($backoffice == 0))
$runjs_code .= '
Expand Down Expand Up @@ -820,10 +833,13 @@ public function hookActionCartSave()
else
$id_product = Tools::getValue('id_product');

if (isset($this->context->cookie->ga_cart))
$gacart = unserialize($this->context->cookie->ga_cart);
else
$gacart = array();
$gacart = array();
if (isset($this->context->cookie->ga_cart)) {
$gacart = json_decode($this->context->cookie->ga_cart, true);
if (false === is_array($gacart)) {
$gacart = array();
}
}

if ($cart['removeAction'] == 'delete')
$ga_products['quantity'] = -1;
Expand All @@ -841,16 +857,9 @@ public function hookActionCartSave()
}

$gacart[$id_product] = $ga_products;
$this->context->cookie->ga_cart = serialize($gacart);
$this->context->cookie->ga_cart = json_encode($gacart);
}
}

public function hookProcessCarrier($params){
if(isset($params['cart']->id_carrier)){
$carrier_name = Db::getInstance()->getValue('SELECT name FROM `'._DB_PREFIX_.'carrier` WHERE id_carrier = '.(int)$params['cart']->id_carrier);
$this->context->cookie->ga_cart .= 'MBG.addCheckoutOption(2,\''.$carrier_name.'\');';
}
}

protected function checkForUpdates()
{
Expand Down
6 changes: 6 additions & 0 deletions views/js/GoogleAnalyticActionLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ var GoogleAnalyticEnhancedECommerce = {
setCurrency: function(Currency) {
ga('set', '&cu', Currency);
},

setCampaign: function(Name,Source,Medium){
ga('set', 'campaignName', Name);
ga('set', 'campaignSource', Source);
ga('set', 'campaignMedium', Medium);
},

add: function(Product, Order, Impression) {
var Products = {};
Expand Down