<modification>
	<id>Add google analytics ecommerce tracking code</id>
	<version>2.1 for OpenCart 1.5.0.x - 1.5.6.x</version>
	<vqmver>2.x</vqmver>
	<author>support@extensadev.com</author>

	<file name="catalog/controller/checkout/success.php">
		<operation>
			<search position="after"><![CDATA[
				public function index() {
			]]></search>
			<add><![CDATA[
				$this->data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
				$this->data['ecommerce_tracking_status'] = false;
				$this->data['order'] = array();
				$this->data['order_products'] = array();

				if ($this->config->get('ecommerce_tracking_status') && $this->config->get('config_google_analytics')) {
					$this->data['ecommerce_tracking_status'] = true;

					if (strpos($this->data['google_analytics'], 'i,s,o,g,r,a,m') !== false) {
						$ecommerce_global_object_pos = strrpos($this->data['google_analytics'], "analytics.js','") + strlen("analytics.js','");
						$this->data['ecommerce_global_object'] = substr($this->data['google_analytics'], $ecommerce_global_object_pos, (strpos($this->data['google_analytics'], "');", $ecommerce_global_object_pos) - $ecommerce_global_object_pos));
						$this->data['start_google_code'] = substr($this->data['google_analytics'], 0, (strpos($this->data['google_analytics'], "pageview');") + strlen("pageview');")));
						$this->data['end_google_code'] = substr($this->data['google_analytics'], (strpos($this->data['google_analytics'], "pageview');") + strlen("pageview');")));
					} else {
						$this->data['ecommerce_global_object'] = false;
						$this->data['start_google_code'] = substr($this->data['google_analytics'], 0, strpos($this->data['google_analytics'], '(function'));
						$this->data['end_google_code'] = substr($this->data['google_analytics'], strpos($this->data['google_analytics'], '(function'));
					}

					if (isset($this->session->data['order_id'])) {
						$order_id = $this->session->data['order_id'];

						$this->load->model('account/order');

						$order_info = $this->model_account_order->getOrder($order_id);

						if ($order_info) {
							$tax = 0;
							$shipping = 0;

							$order_totals = $this->model_account_order->getOrderTotals($order_id);

							foreach ($order_totals as $order_total) {
								if ($order_total['code'] == 'tax') {
									$tax += $order_total['value'];
								} elseif($order_total['code'] == 'shipping') {
									$shipping += $order_total['value'];
								}
							}

							// Data required for _addTrans
							$this->data['order'] = $order_info;
							$this->data['order']['store_name'] = $this->config->get('config_name');
							$this->data['order']['order_total'] = $this->currency->format($order_info['total'], $order_info['currency_code'], $order_info['currency_value'], false);
							$this->data['order']['order_tax'] = $this->currency->format($tax, $order_info['currency_code'], $order_info['currency_value'], false);
							$this->data['order']['order_shipping'] = $this->currency->format($shipping, $order_info['currency_code'], $order_info['currency_value'], false);

							// Data required for _addItem
							$order_products = $this->model_account_order->getOrderProducts($order_id);

							$this->load->model('catalog/product');
							$this->load->model('catalog/category');

							foreach ($order_products as $order_product) {
								$sku = $order_product['product_id'];

								if (($this->config->get('ecommerce_tracking_sku') == 'sku') || ($this->config->get('ecommerce_tracking_sku') == 'sku_option')) {
									$order_product_info = $this->model_catalog_product->getProduct($order_product['product_id']);

									if ($order_product_info && $order_product_info['sku']) {
										$sku = $order_product_info['sku'];
									}
								}

								if (($this->config->get('ecommerce_tracking_sku') == 'id_option') || ($this->config->get('ecommerce_tracking_sku') == 'sku_option')) {
									$order_options = $this->model_account_order->getOrderOptions($order_id, $order_product['order_product_id']);

									foreach ($order_options as $order_option) {
										$sku .= '-' . $order_option['product_option_id'] . ':' . $order_option['product_option_value_id'];

										if ($order_option['type'] != 'file') {
											$option_value = $order_option['value'];
										} else {
											$option_value = utf8_substr($order_option['value'], 0, utf8_strrpos($order_option['value'], '.'));
										}

										$order_product['name'] .= ' - ' . $order_option['name'] . ': ' . (utf8_strlen($option_value) > 20 ? utf8_substr($option_value, 0, 20) . '..' : $option_value);
									}
								}

								$categories = array();

								$order_product_categories = $this->model_catalog_product->getCategories($order_product['product_id']);

								if ($order_product_categories) {
									foreach ($order_product_categories as $order_product_category) {
										$category_data = $this->model_catalog_category->getCategory($order_product_category['category_id']);

										if ($category_data) {
											$categories[] = $category_data['name'];
										}
									}
								}

								$this->data['order_products'][] = array(
									'order_id' => $order_id,
									'sku'      => $sku,
									'name'     => $order_product['name'],
									'category' => implode(',', $categories),
									'quantity' => $order_product['quantity'],
									'price'    => $this->currency->format($order_product['price'] + ($this->config->get('config_tax') ? $order_product['tax'] : 0), $order_info['currency_code'], $order_info['currency_value'], false)
								);
							}
						}
					}
				}
			]]></add>
		</operation>
	</file>

	<file name="catalog/view/theme/*/template/common/success.tpl">
		<operation>
			<search position="before"><![CDATA[
				<?php echo $footer; ?>
			]]></search>
			<add><![CDATA[
				<?php if (isset($ecommerce_tracking_status)) { ?>
					<?php if ($ecommerce_tracking_status && $order && $order_products) { ?>
						<?php echo $start_google_code; ?>

						<?php if ($ecommerce_global_object) { ?>
							<?php echo $ecommerce_global_object; ?>('require', 'ecommerce', 'ecommerce.js');

							<?php echo $ecommerce_global_object; ?>('ecommerce:addTransaction', {
								'id': "<?php echo $order['order_id']; ?>",
								'affiliation': "<?php echo $order['store_name']; ?>",
								'revenue': "<?php echo $order['order_total']; ?>",
								'shipping': "<?php echo $order['order_shipping']; ?>",
								'tax': "<?php echo $order['order_tax']; ?>",
								'currency': "<?php echo $order['currency_code']; ?>"
							});

							<?php foreach($order_products as $order_product) { ?>
							<?php echo $ecommerce_global_object; ?>('ecommerce:addItem', {
								'id': "<?php echo $order_product['order_id']; ?>",
								'name': "<?php echo $order_product['name']; ?>",
								'sku': "<?php echo $order_product['sku']; ?>",
								'category': "<?php echo $order_product['category']; ?>",
								'price': "<?php echo $order_product['price']; ?>",
								'quantity': "<?php echo $order_product['quantity']; ?>"
							});
							<?php } ?>

							<?php echo $ecommerce_global_object; ?>('ecommerce:send');
						<?php } else { ?>
							_gaq.push(['_set', 'currencyCode', '<?php echo $order['currency_code']; ?>']);

							_gaq.push(['_addTrans',
								"<?php echo $order['order_id']; ?>",
								"<?php echo $order['store_name']; ?>",
								"<?php echo $order['order_total']; ?>",
								"<?php echo $order['order_tax']; ?>",
								"<?php echo $order['order_shipping']; ?>",
								"<?php echo $order['payment_city']; ?>",
								"<?php echo $order['payment_zone']; ?>",
								"<?php echo $order['payment_country']; ?>"
							]);

							<?php foreach($order_products as $order_product) { ?>
							_gaq.push(['_addItem',
								"<?php echo $order_product['order_id']; ?>",
								"<?php echo $order_product['sku']; ?>",
								"<?php echo $order_product['name']; ?>",
								"<?php echo $order_product['category']; ?>",
								"<?php echo $order_product['price']; ?>",
								"<?php echo $order_product['quantity']; ?>"
							]);
							<?php } ?>

							_gaq.push(['_trackTrans']);
						<?php } ?>

						<?php echo $end_google_code; ?>
					<?php } else { ?>
						<?php echo $google_analytics; ?>
					<?php } ?>
				<?php } ?>
			]]></add>
		</operation>
	</file>

	<file name="catalog/controller/common/header.php">
		<operation>
			<search position="replace"><![CDATA[
				$this->data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
			]]></search>
			<add><![CDATA[
				if ($this->config->get('ecommerce_tracking_status') && isset($this->request->get['route']) && $this->request->get['route'] == 'checkout/success') {
					$this->data['google_analytics'] = '';
				} else {
					$this->data['google_analytics'] = html_entity_decode($this->config->get('config_google_analytics'), ENT_QUOTES, 'UTF-8');
				}
			]]></add>
		</operation>
	</file>
</modification>