PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/catalog/includes/modules/header_tags/ht_google_analytics.php

https://github.com/chrish123/oscommerce2
PHP | 130 lines | 96 code | 24 blank | 10 comment | 16 complexity | 384f687513d71c05c92b6c3372dea6af MD5 | raw file
  1. <?php
  2. /*
  3. $Id$
  4. osCommerce, Open Source E-Commerce Solutions
  5. http://www.oscommerce.com
  6. Copyright (c) 2010 osCommerce
  7. Released under the GNU General Public License
  8. */
  9. class ht_google_analytics {
  10. var $code = 'ht_google_analytics';
  11. var $title;
  12. var $description;
  13. var $sort_order;
  14. var $enabled = false;
  15. function ht_google_analytics() {
  16. $this->title = MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_TITLE;
  17. $this->description = MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_DESCRIPTION;
  18. if ( defined('MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_STATUS') ) {
  19. $this->sort_order = MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_SORT_ORDER;
  20. $this->enabled = (MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_STATUS == 'True');
  21. }
  22. }
  23. function execute() {
  24. global $PHP_SELF, $oscTemplate, $customer_id;
  25. if (tep_not_null(MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_ID)) {
  26. $header = '<script type="text/javascript">
  27. var _gaq = _gaq || [];
  28. _gaq.push([\'_setAccount\', \'' . tep_output_string(MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_ID) . '\']);
  29. _gaq.push([\'_trackPageview\']);' . "\n";
  30. if ( (MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_EC_TRACKING == 'True') && (basename($PHP_SELF) == FILENAME_CHECKOUT_SUCCESS) && tep_session_is_registered('customer_id') ) {
  31. $order_query = tep_db_query("select orders_id, billing_city, billing_state, billing_country from " . TABLE_ORDERS . " where customers_id = '" . (int)$customer_id . "' order by date_purchased desc limit 1");
  32. if (tep_db_num_rows($order_query) == 1) {
  33. $order = tep_db_fetch_array($order_query);
  34. $totals = array();
  35. $order_totals_query = tep_db_query("select value, class from " . TABLE_ORDERS_TOTAL . " where orders_id = '" . (int)$order['orders_id'] . "'");
  36. while ($order_totals = tep_db_fetch_array($order_totals_query)) {
  37. $totals[$order_totals['class']] = $order_totals['value'];
  38. }
  39. $header .= ' _gaq.push([\'_addTrans\',
  40. \'' . (int)$order['orders_id'] . '\', // order ID - required
  41. \'' . tep_output_string(STORE_NAME) . '\', // store name
  42. \'' . (isset($totals['ot_total']) ? $this->format_raw($totals['ot_total'], DEFAULT_CURRENCY) : 0) . '\', // total - required
  43. \'' . (isset($totals['ot_tax']) ? $this->format_raw($totals['ot_tax'], DEFAULT_CURRENCY) : 0) . '\', // tax
  44. \'' . (isset($totals['ot_shipping']) ? $this->format_raw($totals['ot_shipping'], DEFAULT_CURRENCY) : 0) . '\', // shipping
  45. \'' . tep_output_string_protected($order['billing_city']) . '\', // city
  46. \'' . tep_output_string_protected($order['billing_state']) . '\', // state or province
  47. \'' . tep_output_string_protected($order['billing_country']) . '\' // country
  48. ]);' . "\n";
  49. $order_products_query = tep_db_query("select op.products_id, pd.products_name, op.final_price, op.products_quantity from " . TABLE_ORDERS_PRODUCTS . " op, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_LANGUAGES . " l where op.orders_id = '" . (int)$order['orders_id'] . "' and op.products_id = pd.products_id and l.code = '" . tep_db_input(DEFAULT_LANGUAGE) . "' and l.languages_id = pd.language_id");
  50. while ($order_products = tep_db_fetch_array($order_products_query)) {
  51. $category_query = tep_db_query("select cd.categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " cd, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_LANGUAGES . " l where p2c.products_id = '" . (int)$order_products['products_id'] . "' and p2c.categories_id = cd.categories_id and l.code = '" . tep_db_input(DEFAULT_LANGUAGE) . "' and l.languages_id = cd.language_id limit 1");
  52. $category = tep_db_fetch_array($category_query);
  53. $header .= ' _gaq.push([\'_addItem\',
  54. \'' . (int)$order['orders_id'] . '\', // order ID - required
  55. \'' . (int)$order_products['products_id'] . '\', // SKU/code - required
  56. \'' . tep_output_string($order_products['products_name']) . '\', // product name
  57. \'' . tep_output_string($category['categories_name']) . '\', // category
  58. \'' . $this->format_raw($order_products['final_price']) . '\', // unit price - required
  59. \'' . (int)$order_products['products_quantity'] . '\' // quantity - required
  60. ]);' . "\n";
  61. }
  62. $header .= ' _gaq.push([\'_trackTrans\']); //submits transaction to the Analytics servers' . "\n";
  63. }
  64. }
  65. $header .= ' (function() {
  66. var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
  67. ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
  68. var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
  69. })();
  70. </script>' . "\n";
  71. $oscTemplate->addHeaderTag($header);
  72. }
  73. }
  74. function format_raw($number, $currency_code = '', $currency_value = '') {
  75. global $currencies, $currency;
  76. if (empty($currency_code) || !$currencies->is_set($currency_code)) {
  77. $currency_code = $currency;
  78. }
  79. if (empty($currency_value) || !is_numeric($currency_value)) {
  80. $currency_value = $currencies->currencies[$currency_code]['value'];
  81. }
  82. return number_format(tep_round($number * $currency_value, $currencies->currencies[$currency_code]['decimal_places']), $currencies->currencies[$currency_code]['decimal_places'], '.', '');
  83. }
  84. function isEnabled() {
  85. return $this->enabled;
  86. }
  87. function check() {
  88. return defined('MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_STATUS');
  89. }
  90. function install() {
  91. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Google Analytics Module', 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_STATUS', 'True', 'Do you want to add Google Analytics to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  92. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Google Analytics ID', 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_ID', '', 'The Google Analytics profile ID to track.', '6', '0', now())");
  93. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('E-Commerce Tracking', 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_EC_TRACKING', 'True', 'Do you want to enable e-commerce tracking? (E-Commerce tracking must also be enabled in your Google Analytics profile settings)', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  94. tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
  95. }
  96. function remove() {
  97. tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
  98. }
  99. function keys() {
  100. return array('MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_STATUS', 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_ID', 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_EC_TRACKING', 'MODULE_HEADER_TAGS_GOOGLE_ANALYTICS_SORT_ORDER');
  101. }
  102. }
  103. ?>