['id', 'name', 'price', 'brand', 'category', 'variant', 'list', 'position'], self::DATASET_CLICK => ['id', 'name', 'price', 'brand', 'category', 'variant'], self::DATASET_DETAIL_IMPRESSION => ['id', 'name', 'price', 'brand', 'category', 'variant'], self::DATASET_CART => ['id', 'name', 'price', 'brand', 'category', 'variant', 'quantity'], self::DATASET_CHECKOUT => ['id', 'name', 'price', 'brand', 'category', 'variant', 'quantity'], ]; protected static $_fieldsMap = [ 'id' => ['productId', 'id'], 'name' => 'title', 'price' => 'price', 'brand' => '', 'category' => 'productCategory', 'variant' => '', 'quantity' => 'quantity', 'list' => 'list', 'position' => 'position' ]; protected static $_moneyFields = ['price']; protected static $_appendDescription = false; /** * @param string $description * @return string */ protected static function _getDescription($description) { return ($description) ? ' - ' . trim(strip_tags($description)) : ''; } /** * @param array $data Product Data * @return string */ protected static function _getTitle(array $data) { $title = $data['title']; if (self::$_appendDescription) { $title .= self::_getDescription($data['description']); } return $title; } /* AdWords Dynamic Remarketing */ /** * AdWords Dynamic Remarketing Product View * @param array $data Product Data * @return array */ public static function adGetProductView(array $data) { $values = [ 'ecomm_pagetype' => 'product', 'ecomm_prodid' => $data['id'], 'ecomm_prodname' => $data['title'], 'ecomm_totalvalue' => $data['price'], ]; return [['google_tag_params' => $values]]; } /** * AdWords Dynamic Remarketing Category Page * @param array $products Products List * @return array */ public static function adGetCategoryPage(array $products) { $values = [ 'ecomm_pagetype' => 'category', 'ecomm_prodid' => [], 'ecomm_prodname' => [], ]; if ($products) { foreach ($products as $data) { $values['ecomm_prodid'][] = $data['id']; $values['ecomm_prodname'][] = $data['title']; } } return [['google_tag_params' => $values]]; } /** * AdWords Dynamic Remarketing Add to Shopping Cart * @param array $list * @return array */ public static function adGetShoppingCart(array $list) { $values = [ 'ecomm_pagetype' => 'cart', 'ecomm_prodid' => [], 'ecomm_prodname' => [], 'ecomm_totalvalue' => null ]; $total = '0.00'; foreach ($list as $item) { $values['ecomm_prodid'][] = $item['productId']; $values['ecomm_prodname'][] = self::_getTitle($item); $total = Zend_Locale_Math::Add($total, $item['rowTotal'], 2); } $values['ecomm_totalvalue'] = $total; return [['google_tag_params' => $values]]; } /** * AdWords Dynamic Remarketing Purchase Confirmation * @param array $transaction Transaction Data * @param array $cart Cart Items * @return array */ public static function adGetPurchase(array $transaction, array $cart) { $shippingCost = Zend_Locale_Math::Add($transaction['shippingRate'], $transaction['shippingHandling'], 2); $values = [ 'ecomm_pagetype' => 'purchase', 'ecomm_transactionid' => $transaction['id'], 'ecomm_prodid' => [], 'ecomm_prodname' => [], 'ecomm_prodcategory' => [], 'ecomm_prodprice' => [], 'ecomm_prodqty' => [], 'ecomm_totalvalue' => $transaction['total'], 'ecomm_productsvalue' => null, 'ecomm_tax' => $transaction['tax'], 'ecomm_shipping' => $shippingCost ]; $productsValue = '0.00'; foreach ($cart as $item) { $values['ecomm_prodid'][] = $item['productId']; $values['ecomm_prodname'][] = self::_getTitle($item); $values['ecomm_prodcategory'][] = $item['productCategory']; $values['ecomm_prodprice'][] = number_format($item['price'], 2, '.', ''); $values['ecomm_prodqty'][] = $item['quantity']; $productsValue = Zend_Locale_Math::Add($productsValue, $item['rowTotal'], 2); } $values['ecomm_productsvalue'] = $productsValue; return [['google_tag_params' => $values]]; } /* Google Analytics Ecommerce */ /** * Google Analytics E-commerce * @param array $transaction Transaction Data * @param array $cart Cart Items * @return array */ public static function gaGetECommerce(array $transaction, array $cart) { $shippingCost = Zend_Locale_Math::Add($transaction['shippingRate'], $transaction['shippingHandling'], 2); $values['transactionId'] = $transaction['id']; $values['transactionTotal'] = $transaction['total']; $values['transactionTax'] = $transaction['tax']; $values['transactionShipping'] = $shippingCost; $values['transactionProducts'] = []; foreach ($cart as $item) { $values['transactionProducts'][] = [ 'sku' => $item['productId'], 'name' => self::_getTitle($item), 'category' => $item['productCategory'], 'price' => number_format($item['price'], 2, '.', ''), 'quantity' => $item['quantity'] ]; } return [$values]; } /* Enhanced Ecommerce */ /** * @param string|array $field * @param array $data * @param array $additionalData * @return mixed */ protected static function _getFieldValue($field, array $data, array $additionalData = []) { if (!is_array($field)) { $field = [$field]; } $value = ''; foreach ($field as $_field) { if (!($value = Qs_Array::get($data, $_field, ''))) { $value = Qs_Array::get($additionalData, $_field, ''); } if ($value) { break; } } return $value; } /** * @param array $data Product Data * @param string $dataset Dataset Name * @param array $additionalData Products Additional Product Data * @return array|false */ public static function eeGetProduct(array $data, $dataset, $additionalData = []) { if (self::DATASET_CART == $dataset) { $data['title'] = self::_getTitle($data); } $values = []; foreach (self::$_datasets[$dataset] as $field) { $value = ''; $mapField = self::$_fieldsMap[$field]; if ($mapField) { $value = self::_getFieldValue($mapField, $data, $additionalData); } if ($value) { if (in_array($field, self::$_moneyFields)) { $value = Zend_Locale_Math::round($value, 2); } } $values[$field] = $value; } return array_filter($values); } /** * @param array $list Products List * @param string $dataset Dataset Name * @param array $additionalData Products Additional Product Data * @param bool $saveIndexes Save Indexes from $list * @return array */ public static function eeGetProducts(array $list, $dataset, array $additionalData = [], $saveIndexes = false) { $products = []; foreach ($list as $index => $item) { $values = self::eeGetProduct($item, $dataset, $additionalData); if ($values) { if ($saveIndexes) { $products[$index] = $values; } else { $products[] = $values; } } } return $products; } /** * Enhanced Ecommerce, Measuring Product Impressions * @param array $products Products List * @param string $category Category Title * @param string $list List Title * @param int $startPosition Start Position in List Relative to Page Number * @return array */ public static function eeGetProductImpressions(array $products, $category = null, $list = null, $startPosition = null) { $values = ['impressions' => []]; if ($products) { $position = 1; $additionalData = [ 'productCategory' => $category, 'list' => $list, ]; foreach ($products as $product) { $additionalData['position'] = ($position++) + $startPosition; $value = self::eeGetProduct($product, self::DATASET_IMPRESSION, $additionalData); $values['impressions'][] = $value; } } return [['ecommerce' => $values]]; } /** * Enhanced Ecommerce, Measuring Views of Product Details * @param array $data Product Data * @return array */ public static function eeGetProductDetailImpressions(array $data) { $values = [ 'detail' => [ 'products' => [self::eeGetProduct($data, self::DATASET_DETAIL_IMPRESSION)] ] ]; return [['ecommerce' => $values]]; } /** * Enhanced Ecommerce, Measuring Purchases * @param array $transaction Transaction Data * @param array $cart Cart Items * @return array */ public static function eeGetMeasuringPurchases(array $transaction, array $cart) { $values = [ 'purchase' => [ 'actionField' => [ 'id' => $transaction['id'], 'affiliation' => 'Online Store', 'revenue' => $transaction['total'], 'tax' => $transaction['tax'], 'shipping' => Zend_Locale_Math::Add($transaction['shippingRate'], $transaction['shippingHandling'], 2), //'coupon' => '' ], 'products' => self::eeGetProducts($cart, self::DATASET_CART) ] ]; return [['ecommerce' => $values]]; } }