@@ -51,6 +51,75 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
5151 */
5252 public function run ()
5353 {
54+ // Check if this request is coming to cart controller, we have some product ID and some action to do
55+ if ($ this ->context ->controller ->php_self != 'cart ' ||
56+ !Tools::isSubmit ('id_product ' ) ||
57+ !Tools::isSubmit ('action ' )
58+ ) {
59+ return ;
60+ }
61+
62+ // Resolve action and required parameters
63+ $ id_product = null ;
64+ $ id_product_attribute = null ;
65+ $ event = null ;
66+ $ event_quantity = null ;
67+
68+ /*
69+ * Add to cart from product page
70+ * POST - [id_product] => 1, [qty] => 1, [add] => 1
71+ */
72+ if (Tools::isSubmit ('add ' )) {
73+ $ id_product = (int ) Tools::getValue ('id_product ' );
74+ $ id_product_attribute = 0 ; // This information is not passed, we would need to somehow get it from the attributes
75+ $ event = 'add_to_cart ' ;
76+ $ event_quantity = (int ) Tools::getValue ('qty ' );
77+
78+ /*
79+ * Up from cart page
80+ * GET - [id_product] => 1, [id_product_attribute] => 1
81+ * POST - [qty] => 2, [op] => up
82+ */
83+ } else if (Tools::isSubmit ('op ' ) && Tools::getValue ('op ' ) == 'up ' ) {
84+ $ id_product = (int ) Tools::getValue ('id_product ' );
85+ $ id_product_attribute = (int ) Tools::getValue ('id_product_attribute ' );
86+ $ event = 'add_to_cart ' ;
87+ $ event_quantity = (int ) Tools::getValue ('qty ' );
88+
89+ /*
90+ * Down from cart page
91+ * GET - [id_product] => 1, [id_product_attribute] => 1
92+ * POST - [qty] => 2, [op] => down
93+ */
94+ } else if (Tools::isSubmit ('op ' ) && Tools::getValue ('op ' ) == 'down ' ) {
95+ $ id_product = (int ) Tools::getValue ('id_product ' );
96+ $ id_product_attribute = (int ) Tools::getValue ('id_product_attribute ' );
97+ $ event = 'remove_from_cart ' ;
98+ $ event_quantity = (int ) Tools::getValue ('qty ' );
99+
100+ /*
101+ * Delete from cart page
102+ * GET - [id_product] => 1, [id_product_attribute] => 1, [delete] => 1
103+ */
104+ } else if (Tools::isSubmit ('delete ' )) {
105+ $ id_product = (int ) Tools::getValue ('id_product ' );
106+ $ id_product_attribute = (int ) Tools::getValue ('id_product_attribute ' );
107+ $ event = 'remove_from_cart ' ;
108+ $ event_quantity = 1 ; // We have no easy way to distinguish this
109+ }
110+
111+ // Get our tag handler
112+ $ ganalyticsDataHandler = new GanalyticsDataHandler (
113+ $ this ->context ->cart ->id ,
114+ $ this ->context ->shop ->id
115+ );
116+
117+ $ ganalyticsDataHandler ->persistData ("Event " . $ event . ", id_product " . $ id_product . ", id_product_attribute " . $ id_product_attribute . ", event_quantity " . $ event_quantity . "" );
118+ return ;
119+
120+ // Render the event
121+ // $this->gaScripts .= $this->module->getTools()->renderEvent('sign_up', []);
122+
54123 if (!isset ($ this ->context ->cart )) {
55124 return ;
56125 }
@@ -113,7 +182,7 @@ public function run()
113182 $ productId = Tools::getValue ('id_product ' );
114183 }
115184
116- $ gaCart = $ ganalyticsDataHandler ->manageData ( '' , ' R ' );
185+ $ gaCart = $ ganalyticsDataHandler ->readData ( );
117186
118187 if ($ cart ['removeAction ' ] == 'delete ' ) {
119188 $ gaProducts ['quantity ' ] = -1 ;
@@ -130,7 +199,7 @@ public function run()
130199 }
131200
132201 $ gaCart [$ productId ] = $ gaProducts ;
133- $ ganalyticsDataHandler ->manageData ($ gaCart, ' W ' );
202+ $ ganalyticsDataHandler ->persistData ($ gaCart );
134203 }
135204 }
136205}
0 commit comments