uc_googleanalytics.module 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @file
  4. * Adds Google Analytics Javascript to the checkout completion page.
  5. *
  6. * Adds the required Javascript to the checkout completion page to allow
  7. * e-commerce statistics tracking through Google Analytics.
  8. *
  9. * Refer to http://code.google.com/apis/analytics/docs/gaTrackingEcommerce.html
  10. * for documentation on the functions used to submit e-commerce statistics to
  11. * Google Analytics.
  12. */
  13. /**
  14. * Check which version of google analytics code is used on the site.
  15. */
  16. function uc_googleanalytics_flush_caches() {
  17. $info = system_get_info('module', 'googleanalytics');
  18. if (preg_match('|7\.x\-[2-9]\.[0-9x]+|', $info['version'])) {
  19. variable_set('uc_googleanalytics_version', 'analytics.js');
  20. }
  21. else {
  22. variable_set('uc_googleanalytics_version', 'ga.js');
  23. }
  24. }
  25. /**
  26. * Implements hook_page_alter().
  27. */
  28. function uc_googleanalytics_page_alter(&$page) {
  29. // Check to see if we are at the order completion page.
  30. if (uc_googleanalytics_display()) {
  31. // If so, then if we can load the order...
  32. if (!empty($_SESSION['ucga_order_id']) && $order = uc_order_load($_SESSION['ucga_order_id'])) {
  33. // Build the GA tracking code.
  34. $script = uc_googleanalytics_ecommerce_js($order);
  35. // Add the code to the footer.
  36. drupal_add_js($script, array('type' => 'inline', 'scope' => 'footer', 'preprocess' => FALSE));
  37. }
  38. // Clean out the session variable.
  39. if (isset($_SESSION['ucga_order_id'])) {
  40. unset($_SESSION['ucga_order_id']);
  41. }
  42. }
  43. }
  44. /**
  45. * Implements hook_uc_order().
  46. */
  47. function uc_googleanalytics_uc_order($op, $order, $arg2) {
  48. // If a new order is created during the checkout process...
  49. if ($op == 'new') {
  50. // Store the order ID for later use.
  51. $_SESSION['ucga_order_id'] = $order->order_id;
  52. }
  53. }
  54. /**
  55. * Determine whether or not to display the e-commerce related JS through GA.
  56. *
  57. * @return bool
  58. * TRUE or FALSE indicating whether or not to display the GA e-commerce JS.
  59. */
  60. function uc_googleanalytics_display() {
  61. // Display the GA e-commerce JS if the URL is cart/checkout/complete...
  62. if (arg(0) == 'cart' && arg(1) == 'checkout' && arg(2) == 'complete') {
  63. return TRUE;
  64. }
  65. // Or if the URL is the custom completion page.
  66. $completion_page = variable_get('uc_cart_checkout_complete_page', '');
  67. if (!empty($completion_page) && $completion_page == drupal_get_path_alias($_GET['q'])) {
  68. return TRUE;
  69. }
  70. // Or if another module says this is the page through hook_ucga_display().
  71. foreach (module_invoke_all('ucga_display') as $result) {
  72. if ($result === TRUE) {
  73. return TRUE;
  74. }
  75. }
  76. // Otherwise return FALSE.
  77. return FALSE;
  78. }
  79. /**
  80. * Build the e-commerce JS passed to Google Analytics for order tracking.
  81. *
  82. * @param $order
  83. * The fully loaded order object to convert into GA JS.
  84. *
  85. * @return string
  86. * The JS that should be added to the page footer.
  87. */
  88. function uc_googleanalytics_ecommerce_js($order) {
  89. $analytics_version = variable_get('uc_googleanalytics_version', 'ga.js');
  90. if ($analytics_version == 'analytics.js') {
  91. $script = 'ga("require", "ecommerce", "ecommerce.js");';
  92. }
  93. else {
  94. $script = '';
  95. }
  96. // Lookup the name of the country or default to the ID if it can't be found
  97. // for some reason.
  98. if ($country_data = uc_get_country_data(array('country_id' => $order->billing_country))) {
  99. $order->billing_country_name = $country_data[0]['country_name'];
  100. }
  101. else {
  102. $order->billing_country_name = $order->billing_country;
  103. }
  104. // Lookup the name of the zone.
  105. $order->billing_zone_name = uc_zone_get_by_id($order->billing_zone);
  106. // Calculate order tax and shipping totals.
  107. $order->tax_total = 0;
  108. $order->shipping_total = 0;
  109. foreach ($order->line_items as $line_item) {
  110. if ($line_item['type'] == 'tax') {
  111. $order->tax_total += $line_item['amount'];
  112. }
  113. elseif ($line_item['type'] == 'shipping') {
  114. $order->shipping_total += $line_item['amount'];
  115. }
  116. }
  117. // Build the transaction arguments.
  118. $trans = array(
  119. 'order_id' => $order->order_id,
  120. 'store' => uc_store_name(),
  121. 'total' => $order->order_total,
  122. 'tax' => $order->tax_total,
  123. 'shipping' => $order->shipping_total,
  124. 'city' => $order->billing_city,
  125. 'state' => $order->billing_zone_name,
  126. 'country' => $order->billing_country_name,
  127. );
  128. // Allow modules to alter the transaction arguments.
  129. drupal_alter('ucga_trans', $trans, $order);
  130. // Create GA-friendly associative array.
  131. $script_args = array(
  132. 'id' => $trans['order_id'],
  133. 'affiliation' => $trans['store'],
  134. 'revenue' => $trans['total'],
  135. 'tax' => $trans['tax'],
  136. 'shipping' => $trans['shipping'],
  137. 'city' => $trans['city'],
  138. 'region' => $trans['state'],
  139. 'country' => $trans['country'],
  140. );
  141. // Add the transaction line to the JS.
  142. if ($analytics_version == 'analytics.js') {
  143. $script .= 'ga("ecommerce:addTransaction", ' . drupal_json_encode($script_args) . ');';
  144. }
  145. else {
  146. foreach ($script_args as &$arg) {
  147. $arg = drupal_json_encode($arg);
  148. }
  149. $script .= '_gaq.push(["_addTrans", ' . implode(', ', $script_args) . ']);';
  150. }
  151. // Loop through the products on the order.
  152. foreach ($order->products as $product) {
  153. $product->category = '';
  154. // Try to find a category (term) for the product. Since products most often
  155. // only have one category, the first one returned (in the
  156. // $node->taxonomy_catalog) is chosen.
  157. if (module_exists('taxonomy')) {
  158. $node = node_load($product->nid);
  159. if (isset($node->taxonomy_catalog[LANGUAGE_NONE][0]['tid'])) {
  160. $term = taxonomy_term_load($node->taxonomy_catalog[LANGUAGE_NONE][0]['tid']);
  161. $product->category = $term->name;
  162. }
  163. }
  164. if (empty($product->category)) {
  165. $product->category = t('No category');
  166. }
  167. // Build the item arguments.
  168. $item = array(
  169. 'order_id' => $order->order_id,
  170. 'sku' => $product->model,
  171. 'name' => $product->title,
  172. 'category' => $product->category,
  173. 'price' => $product->price,
  174. 'qty' => $product->qty,
  175. );
  176. // Allow modules to alter the item arguments.
  177. drupal_alter('ucga_item', $item, $product, $trans, $order);
  178. // Create GA-friendly associative array.
  179. $script_args = array(
  180. 'id' => $item['order_id'],
  181. 'sku' => $item['sku'],
  182. 'name' => $item['name'],
  183. 'category' => (string) $item['category'],
  184. 'price' => $item['price'],
  185. 'quantity' => $item['qty'],
  186. );
  187. // Add the item line to the JS.
  188. if ($analytics_version == 'analytics.js') {
  189. $script .= 'ga("ecommerce:addItem", ' . drupal_json_encode($script_args) . ');';
  190. }
  191. else {
  192. foreach ($script_args as &$arg) {
  193. $arg = drupal_json_encode($arg);
  194. }
  195. $script .= '_gaq.push(["_addItem", ' . implode(', ', $script_args) . ']);';
  196. }
  197. }
  198. // Add the function to submit the transaction to GA.
  199. if ($analytics_version == 'analytics.js') {
  200. $script .= 'ga("ecommerce:send");';
  201. }
  202. else {
  203. $script .= '_gaq.push(["_trackTrans"]);';
  204. }
  205. return $script;
  206. }