From f9f6d14cd831f48efd8724a7e9bd2d225b3e9b54 Mon Sep 17 00:00:00 2001
From: hf <fei.hong@yoho.cn>
Date: Tue, 5 Apr 2016 17:22:58 +0800
Subject: [PATCH] code review by fei.hong: do add criteo analysis codes

---
 library/WebPlugin/Helpers.php                                          |  29 +++++++++++++++++++++++++++--
 template/www.yohobuy.com/actions/cart/index/cart.phtml                 |  36 ++++++++++++++++++++++++++++++++++++
 yohobuy/www.yohobuy.com/application/models/Shopping/Cart.php           | 100 +++++++++++++++++++++++++++++++++++++++++++++++-----------------------------------------------------
 yohobuy/www.yohobuy.com/application/modules/Cart/controllers/Index.php |   1 +
 4 files changed, 111 insertions(+), 55 deletions(-)

diff --git a/library/WebPlugin/Helpers.php b/library/WebPlugin/Helpers.php
index 2297ca2..e64fbfc 100644
--- a/library/WebPlugin/Helpers.php
+++ b/library/WebPlugin/Helpers.php
@@ -682,10 +682,12 @@ class Helpers
      *
      * @param array $cartGoods 购物车商品列表
      * @param bool $isAdvanceCart 是否是预售购物车(和上市期有关)
-     * @param boolean $isValid 是否是可用商品(非失效商品),默认是
+     * @param bool $isValid 是否是可用商品(非失效商品),默认是
+     * @param bool $isOffShelves 是否卖光了, 默认否
+     * @param array $analysis 第三方数据分析用的
      * @return array 处理之后的购物车商品数据
      */
-    public static function formatCartGoods($cartGoods, $isAdvanceCart, $isValid = true, $isOffShelves = false)
+    public static function formatCartGoods($cartGoods, $isAdvanceCart, $isValid = true, $isOffShelves = false, &$analysis = array())
     {
         $result = array();
 
@@ -744,6 +746,29 @@ class Helpers
             elseif ($value['real_price'] == 0) {
                 $oneGoods['productPrice'] = self::transPrice($value['sales_price']);
                 $oneGoods['xForOne'] = true;
+                
+                // 分析用: 商品ID列表
+                $analysis['ids'][] = $value['product_id'];
+                // 分析用: 品友
+                $analysis['ipinyou'] .= $value['product_id'] . ',' . $value['buy_number'] . ';';
+                // 分析用: CRITEO
+                $analysis['criteo'][] = array(
+                    'id' => $value['product_skn'],
+                    'quantity' => intval($value['buy_number']),
+                    'price' => $value['last_vip_price'],
+                );
+            } 
+            else {
+                // 分析用: 商品ID列表
+                $analysis['ids'][] = $value['product_id'];
+                // 分析用: 品友
+                $analysis['ipinyou'] .= $value['product_id'] . ',' . $value['buy_number'] . ';';
+                // 分析用: CRITEO
+                $analysis['criteo'][] = array(
+                    'id' => $value['product_skn'],
+                    'quantity' => intval($value['buy_number']),
+                    'price' => $value['last_vip_price'],
+                );
             }
 
             // 商品类型:预售或普通
diff --git a/template/www.yohobuy.com/actions/cart/index/cart.phtml b/template/www.yohobuy.com/actions/cart/index/cart.phtml
index 60c0fff..b216108 100644
--- a/template/www.yohobuy.com/actions/cart/index/cart.phtml
+++ b/template/www.yohobuy.com/actions/cart/index/cart.phtml
@@ -341,4 +341,40 @@
     <div class="loading"><span></span>请稍后...</div>
 </div>
 <!-- 商品详细信息窗口 -->
+
+<script type="text/javascript"> 
+(function (d) {	 
+window.bd_cpro_rtid="P1fsPWc";	 
+var s = d.createElement("script");s.type = "text/javascript";s.async = true;s.src = location.protocol + "//cpro.baidu.com/cpro/ui/rt.js";	 
+var s0 = d.getElementsByTagName("script")[0];s0.parentNode.insertBefore(s, s0); 
+})(document);	 
+</script>
+<script type="text/javascript" src="http://static.criteo.net/js/ld/ld.js" async="true"></script>
+<script type="text/javascript">
+window.criteo_q = window.criteo_q || [];
+window.criteo_q.push(
+{event:"setAccount", account: 16184},
+{event:"setCustomerId", id: "{{uid}}"},
+{event:"setSiteType", type: "d"}, 
+{event:"viewBasket", item: {{{cartEnsure.criteo}}} }
+);
+</script>
+<!-- 聚效 -->
+<script type="text/javascript">
+var _mvq = window._mvq || []; 
+window._mvq = _mvq;
+_mvq.push(['$setAccount', 'm-23428-1']);
+_mvq.push(['$setGeneral', 'cartview', '', /*用户名*/ '', '{{uid}}']);
+_mvq.push(['$logConversion']);
+_mvq.push(['$addItem', '', /*商品id*/ '{{cartEnsure.ids}}', '', '']);
+_mvq.push(['$logData']);
+</script>
+<script>
+!function(w,d,e){
+var _money='{{cartEnsure.ipinyouAmount}}';
+var _productList='{{cartEnsure.ipinyou}}';
+var b=location.href,c=d.referrer,f,s,g=d.cookie,h=g.match(/(^|;)\s*ipycookie=([^;]*)/),i=g.match(/(^|;)\s*ipysession=([^;]*)/);if (w.parent!=w){f=b;b=c;c=f;};u='//stats.ipinyou.com/cvt?a='+e('MC.Ok.7NFMIlCH_F_LE2riRlF2r_')+'&c='+e(h?h[2]:'')+'&s='+e(i?i[2].match(/jump\%3D(\d+)/)[1]:'')+'&u='+e(b)+'&r='+e(c)+'&rd='+(new Date()).getTime()+'&Money='+e(_money)+'&ProductList='+e(_productList)+'&e=';
+function _(){if(!d.body){setTimeout(_(),100);}else{s= d.createElement('script');s.src = u;d.body.insertBefore(s,d.body.firstChild);}}_();
+}(window,document,encodeURIComponent);
+</script>
 {{> layout/footer}}
\ No newline at end of file
diff --git a/yohobuy/www.yohobuy.com/application/models/Shopping/Cart.php b/yohobuy/www.yohobuy.com/application/models/Shopping/Cart.php
index ea1ae79..4191bb2 100644
--- a/yohobuy/www.yohobuy.com/application/models/Shopping/Cart.php
+++ b/yohobuy/www.yohobuy.com/application/models/Shopping/Cart.php
@@ -36,6 +36,9 @@ class CartModel
     public static function myCartData($uid, $shoppingKey, $cartDelList)
     {
         $result = array();
+        
+        // 存放分析用的数据
+        $analysisData = array('ipinyou' => '');
 
         do {
             $result['isEmpty'] = false;
@@ -47,7 +50,7 @@ class CartModel
 
             // 调用接口,获取购物车数据
             $cartData = CartData::cartData($uid, $shoppingKey);
-            
+
             // 接口异常时,购物车置为空
             if (empty($cartData['data'])) {
                 $result['isEmpty'] = true;
@@ -55,7 +58,7 @@ class CartModel
                 $result['viewOrderUrl'] = Helpers::url('/home/orders', array('t' => time()));
                 break;
             }
-            
+
             $ordinaryCartData = $cartData['data']['ordinary_cart_data'];
             $advanceCartData = $cartData['data']['advance_cart_data'];
 
@@ -71,7 +74,7 @@ class CartModel
             $ordinaryOffShelves = empty($ordinaryCartData['off_shelves_goods_list']);
             // 预售商品是否有失效的
             $advanceOffShelves = empty($advanceCartData['off_shelves_goods_list']);
-            
+
             /* 移除的商品列表 */
             if (!empty($cartDelList)) {
                 $result['deleteShop'] = json_decode($cartDelList, true);
@@ -121,44 +124,36 @@ class CartModel
             // 普通的
             if (!empty($ordinaryCartData['goods_list'])) {
                 $result['commonSell']['cartProductNum'] = strval($ordinaryCount);
-                $result['commonSell']['productItem'] = Helpers::formatCartGoods($ordinaryCartData['goods_list'], false, true);
+                $result['commonSell']['productItem'] = Helpers::formatCartGoods($ordinaryCartData['goods_list'], false, true, false, $analysisData);
             }
             // 预售的
             if (!empty($advanceCartData['goods_list'])) {
                 $result['preSell']['cartProductNum'] = strval($advanceCount);
-                $result['preSell']['productItem'] = Helpers::formatCartGoods($advanceCartData['goods_list'], true, true);
+                $result['preSell']['productItem'] = Helpers::formatCartGoods($advanceCartData['goods_list'], true, true, false, $analysisData);
             }
 
             /* 已售磬失效 */
             // 普通的
             if (!$ordinarySoldOut) {
                 //$result['commonSell']['cartProductNum'] += count($ordinaryCartData['sold_out_goods_list']);
-                $result['commonSell']['productItem'] = isset($result['commonSell']['productItem']) 
-                                                     ? self::appendProductItem($result['commonSell']['productItem'], Helpers::formatCartGoods($ordinaryCartData['sold_out_goods_list'], false, true), count($result['commonSell']['productItem'])) 
-                                                     : Helpers::formatCartGoods($ordinaryCartData['sold_out_goods_list'], false, true);
+                $result['commonSell']['productItem'] = isset($result['commonSell']['productItem']) ? self::appendProductItem($result['commonSell']['productItem'], Helpers::formatCartGoods($ordinaryCartData['sold_out_goods_list'], false, true), count($result['commonSell']['productItem'])) : Helpers::formatCartGoods($ordinaryCartData['sold_out_goods_list'], false, true, false, $analysisData);
             }
             // 预售的
             if (!$advanceSoldOut) {
                 //$result['preSell']['cartProductNum'] += count($advanceCartData['sold_out_goods_list']);
-                $result['preSell']['productItem'] = isset($result['preSell']['productItem']) 
-                                                  ? self::appendProductItem($result['preSell']['productItem'], Helpers::formatCartGoods($advanceCartData['sold_out_goods_list'], true, true), count($result['preSell']['productItem'])) 
-                                                  : Helpers::formatCartGoods($advanceCartData['sold_out_goods_list'], true, true);
+                $result['preSell']['productItem'] = isset($result['preSell']['productItem']) ? self::appendProductItem($result['preSell']['productItem'], Helpers::formatCartGoods($advanceCartData['sold_out_goods_list'], true, true), count($result['preSell']['productItem'])) : Helpers::formatCartGoods($advanceCartData['sold_out_goods_list'], true, true, false, $analysisData);
             }
-            
+
             /* 已失效的 */
             // 普通的
             if (!$ordinaryOffShelves) {
                 //$result['commonSell']['cartProductNum'] += count($ordinaryCartData['off_shelves_goods_list']);
-                $result['commonSell']['productItem'] = isset($result['commonSell']['productItem']) 
-                                                     ? self::appendProductItem($result['commonSell']['productItem'], Helpers::formatCartGoods($ordinaryCartData['off_shelves_goods_list'], false, true, true), count($result['commonSell']['productItem'])) 
-                                                     : Helpers::formatCartGoods($ordinaryCartData['off_shelves_goods_list'], false, true, true);
+                $result['commonSell']['productItem'] = isset($result['commonSell']['productItem']) ? self::appendProductItem($result['commonSell']['productItem'], Helpers::formatCartGoods($ordinaryCartData['off_shelves_goods_list'], false, true, true), count($result['commonSell']['productItem'])) : Helpers::formatCartGoods($ordinaryCartData['off_shelves_goods_list'], false, true, true, $analysisData);
             }
             // 预售的
             if (!$advanceOffShelves) {
                 //$result['preSell']['cartProductNum'] += count($advanceCartData['off_shelves_goods_list']);
-                $result['preSell']['productItem'] = isset($result['preSell']['productItem']) 
-                                                  ? self::appendProductItem($result['preSell']['productItem'], Helpers::formatCartGoods($advanceCartData['off_shelves_goods_list'], true, true, true), count($result['preSell']['productItem'])) 
-                                                  : Helpers::formatCartGoods($advanceCartData['off_shelves_goods_list'], true, true, true);
+                $result['preSell']['productItem'] = isset($result['preSell']['productItem']) ? self::appendProductItem($result['preSell']['productItem'], Helpers::formatCartGoods($advanceCartData['off_shelves_goods_list'], true, true, true), count($result['preSell']['productItem'])) : Helpers::formatCartGoods($advanceCartData['off_shelves_goods_list'], true, true, true, $analysisData);
             }
 
             /* 赠品 */
@@ -168,22 +163,17 @@ class CartModel
             }
             // 普通的
             if (!empty($ordinaryCartData['gift_list'])) {
-                $result['subjoinItem'] = isset($result['subjoinItem']) ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($ordinaryCartData['gift_list'], true), count($result['subjoinItem'])) 
-                                                                       : Helpers::formatGiftPriceGoods($ordinaryCartData['gift_list'], true);
+                $result['subjoinItem'] = isset($result['subjoinItem']) ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($ordinaryCartData['gift_list'], true), count($result['subjoinItem'])) : Helpers::formatGiftPriceGoods($ordinaryCartData['gift_list'], true);
             }
 
             /* 加价购 */
             // 预售的
             if (!empty($advanceCartData['price_gift'])) {
-                $result['subjoinItem'] = isset($result['subjoinItem']) 
-                                       ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($advanceCartData['price_gift'], false), count($result['subjoinItem'])) 
-                                       : Helpers::formatGiftPriceGoods($advanceCartData['price_gift'], false);
+                $result['subjoinItem'] = isset($result['subjoinItem']) ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($advanceCartData['price_gift'], false), count($result['subjoinItem'])) : Helpers::formatGiftPriceGoods($advanceCartData['price_gift'], false);
             }
             // 普通的
             if (!empty($ordinaryCartData['price_gift'])) {
-                $result['subjoinItem'] = isset($result['subjoinItem']) 
-                                       ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($ordinaryCartData['price_gift'], false), count($result['subjoinItem'])) 
-                                       : Helpers::formatGiftPriceGoods($ordinaryCartData['price_gift'], false);
+                $result['subjoinItem'] = isset($result['subjoinItem']) ? self::appendProductItem($result['subjoinItem'], Helpers::formatGiftPriceGoods($ordinaryCartData['price_gift'], false), count($result['subjoinItem'])) : Helpers::formatGiftPriceGoods($ordinaryCartData['price_gift'], false);
             }
 
             /* 促销短语 */
@@ -197,13 +187,19 @@ class CartModel
                 //$result['salesPromotion'] = array_merge($result['salesPromotion'], self::buildPromotionData($advanceCartData['promotion_info']));
                 $result['salesPromotion'] = self::buildPromotionData($advanceCartData['promotion_info']);
             }
-            
+
             // 清空变量
             $advanceCartData = array();
             $ordinaryCartData = array();
             $cartData = array();
             
         } while (false);
+        
+        // 增加第三方分析用的数据
+        $result['ids'] = empty($analysisData['ids']) ? '' : implode(',', $analysisData['ids']);
+        $result['ipinyou'] = empty($analysisData['ipinyou']) ? '' : $analysisData['ipinyou'];
+        $result['ipinyouAmount'] = isset($result['productAllA']) ? $result['productAllA'] : '0.00';
+        $result['criteo'] = empty($analysisData['criteo']) ? '' : json_encode($analysisData['criteo']);
 
         return $result;
     }
@@ -423,7 +419,7 @@ class CartModel
                 break;
             }
 
-            
+
             $result['data']['hasPrev'] = false;
             $result['data']['hasNext'] = true;
             if (intval($page) > 1) {
@@ -476,20 +472,20 @@ class CartModel
             if (empty($sknList)) {
                 break;
             }
-            
+
             // 组装 skn => goodsId 对应关系
             $sknArr = array();
             foreach ($sknList as $value) {
                 $value = explode('-', $value);
                 if (isset($value[1])) {
-                    $sknArr[ $value[0] ] = $value[1];
+                    $sknArr[$value[0]] = $value[1];
                 }
             }
-            
+
             if (array() === $sknArr) {
                 break;
             }
-            
+
             // 通过SKN列表搜索调用商品信息
             $browse = CartData::browseRecordFromSearch(implode(' ', array_keys($sknArr)));
             if (empty($browse['data']['product_list'])) {
@@ -532,11 +528,11 @@ class CartModel
                     $build['href'] = Helpers::url('/product/pro_' . $value['product_id'] . '_' . $goodsId . '/' . $value['cn_alphabet'] . '.html', null, 'item');
 
                     $result['data']['item'][] = $build;
-                    
+
                     break;
                 }
             }
-            
+
             // 当数据量不足6个时,判定为没有下一页
             if (!isset($result['data']['item'][5])) {
                 $result['data']['hasNext'] = false;
@@ -574,7 +570,7 @@ class CartModel
                     if ($value['payment_type'] == 1 && $value['is_support'] === 'Y') {
                         $result['onlinePay']['checked'] = $isDefault ? true : false;
                         $result['onlinePay']['paymentId'] = $value['payment_id'];
-                    } 
+                    }
                     // 货到付款
                     elseif ($value['payment_type'] == 2 && $value['is_support'] === 'Y') {
                         $result['deliveryPay']['checked'] = $isDefault ? true : false;
@@ -586,7 +582,7 @@ class CartModel
                         $result['defaultPayWay'] = $value['payment_type_name'];
                     }
                 }
-            } 
+            }
 
             // 未设置时,设置默认
             if (!isset($result['defaultPayWay'])) {
@@ -597,7 +593,7 @@ class CartModel
                 $result['defaultPayWay'] = '在线支付';
                 $result['supportDeliveryPay'] = true;
             }
-            
+
             if (!empty($pay['data']['delivery_time'])) {
                 $build = array();
                 foreach ($pay['data']['delivery_time'] as $value) {
@@ -610,8 +606,8 @@ class CartModel
                     }
                     $result['delivery'][] = $build;
                 }
-            } 
-            
+            }
+
             // 默认
             if (!isset($result['defaultDelivery'])) {
                 $result['defaultDelivery'] = '只工作日送货(双休日、节假日不送)';
@@ -619,7 +615,7 @@ class CartModel
                     $result['delivery'][0]['checked'] = true;
                 }
             }
-            
+
             // 支付支持的平台
             $result['supportLine'] = array(
                 array('src' => 'http://static.yohobuy.com/images/pay/icon/zhifubao.png'),
@@ -738,7 +734,7 @@ class CartModel
             $result['isPreSell'] = $isAdvanceCart ? true : false;
             // 顺丰查看详情
             $result['sfUrl'] = Helpers::url('/help/index/logisticsarea', null, 'default');
-            
+
             // 是否是新客访问, 控制显示新手引导
             $isNewUser = Helpers::isNewUser();
             // 新用户(未下单)且未设置收货地址的,提示引导
@@ -746,7 +742,6 @@ class CartModel
                 $isNewUser = false;
             }
             $result['isNewUser'] = $isNewUser ? true : false;
-                
         } while (false);
 
         return $result;
@@ -904,7 +899,7 @@ class CartModel
                 $phone = explode('-', strtr($value['phone'], array('null' => '')));
                 $mobile = strtr($value['mobile'], array('null' => ''));
                 $mobile = substr($mobile, 0, 3) . '****' . substr($mobile, 7);
-                
+
                 $build = array();
                 $build['id'] = $value['address_id'];
                 $build['user'] = $value['consignee'];
@@ -1007,7 +1002,7 @@ class CartModel
         } else if (!empty($phoneNum) && !is_numeric($phoneNum)) {
             $result['code'] = 406;
             $result['message'] = '输入的固定电话格式不正确';
-        } else if ((!empty($phoneNum) && empty($phoneCode)) || (!empty($phoneCode) && empty($phoneNum)) ) {
+        } else if ((!empty($phoneNum) && empty($phoneCode)) || (!empty($phoneCode) && empty($phoneNum))) {
             $result['code'] = 407;
             $result['message'] = '输入的固定电话格式不正确';
         } else {
@@ -1105,7 +1100,6 @@ class CartModel
                 $result[] = $build;
             }
             $couponList = array();
-            
         } while (false);
 
         return $result;
@@ -1140,7 +1134,7 @@ class CartModel
 
         return $result;
     }
-    
+
     /**
      * 获取购物车总数
      * 
@@ -1152,7 +1146,7 @@ class CartModel
     {
         return CartData::cartCount($uid, $shoppingKey);
     }
-    
+
     /**
      * 追加商品
      * 
@@ -1165,9 +1159,9 @@ class CartModel
     {
         foreach ($data as $value) {
             $source[$index] = $value;
-            ++ $index;
+            ++$index;
         }
-        
+
         return $source;
     }
 
@@ -1178,10 +1172,10 @@ class CartModel
      * @param int $default 默认值
      * @return float
      */
-    private static function genProductAmount($promotionList, $default) 
+    private static function genProductAmount($promotionList, $default)
     {
         $result = $default;
-        
+
         $vipAmount = null;
         foreach ($promotionList as $value) {
             if ($value['promotion'] === 'VIP优惠') {
@@ -1193,10 +1187,10 @@ class CartModel
             $result = floatval($default) - floatval($vipAmount);
         }
         $result = Helpers::transPrice($result);
-        
+
         return $result;
     }
-    
+
     /**
      * 构建促销短语数据
      * 
diff --git a/yohobuy/www.yohobuy.com/application/modules/Cart/controllers/Index.php b/yohobuy/www.yohobuy.com/application/modules/Cart/controllers/Index.php
index 5e2cca9..ef441ac 100644
--- a/yohobuy/www.yohobuy.com/application/modules/Cart/controllers/Index.php
+++ b/yohobuy/www.yohobuy.com/application/modules/Cart/controllers/Index.php
@@ -35,6 +35,7 @@ class IndexController extends WebAction
         $this->_view->display('cart', array(
             'cartEnsurePage' => true,
             'cartEnsure' => CartModel::myCartData($uid, $shoppingKey, $cartDelList),
+            'uid' => $uid ? $uid : '',
         ));
     }
     
--
libgit2 0.24.0