Jump to content

Google Analytics - add purchase on order status change


Recommended Posts

Hello!

I'm trying to send informations about transaction to Universal Analytics when the order status changes to specified one. It's all because of customers sometimes are not coming onto thank you page, they're closing website right after payment becomes done (external express payments provider). Used GA module is the official one provided by PrestaShop. And used Presta version is 1.6.

One of my attempts is:

	public function hookActionOrderHistoryAddAfter($params)
	{
		$orderHistory = $params['order_history'];

        $new_id_order_state = (int) $orderHistory->id_order_state;
        $id_order = $orderHistory->id_order;

		if ($new_id_order_state == 18)
		{
-- HERE BEGINS GA CODE FROM ORDER CONFIRMATION HOOK --
			$order = new Order($id_order);
			$products = $order->getProducts();

			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);
				if ($ga_order_sent === false)
				{
					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)
					{
						$order_products = array();
						$cart = new Cart($order->id_cart);
						foreach ($cart->getProducts() as $order_product)
							$order_products[] = $this->wrapProduct($order_product, array(), 0, true);

						$transaction = array(
							'id' => $order->id,
							'affiliation' => (version_compare(_PS_VERSION_, '1.5', '>=') && Shop::isFeatureActive()) ? $this->context->shop->name : Configuration::get('PS_SHOP_NAME'),
							'revenue' => $order->total_paid,
							'shipping' => $order->total_shipping,
							'tax' => $order->total_paid_tax_incl - $order->total_paid_tax_excl,
							'url' => $this->context->link->getModuleLink('ganalytics', 'ajax', array(), true),
							'customer' => $order->id_customer);
						$ga_scripts = $this->addTransaction($order_products, $transaction);

						$this->js_state = 1;
						return $this->_runJs($ga_scripts);
					}
				}
			}
		}
	}

Of course I've properly added registerHook, reinstaled module etc.

Unfortunately, after status change, nothing happens. Orders are still missing from Analytics.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...