uc_googleanalytics.api.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Google Analytics for Ubercart module.
  5. */
  6. /**
  7. * @addtogroup hooks
  8. * @{
  9. */
  10. /**
  11. * Determines whether e-commerce tracking code should be added to the page.
  12. *
  13. * The Google Analytics module takes care of adding the necessary .js file from
  14. * Google for tracking general statistics. The UC Google Analytics module works
  15. * in conjunction with this code to add e-commerce specific code. However, the
  16. * e-commerce code should only be added on appropriate pages. Generally, the
  17. * correct page will be the checkout completion page at cart/checkout/complete.
  18. * However, because modules can change the checkout flow as necessary, it must
  19. * be possible for alternate pages to be used.
  20. *
  21. * This hook allows other modules to tell the UC Google Analytics module that
  22. * it should go ahead and add the e-commerce tracking code to the current page.
  23. * A module simply needs to implement this hook and return TRUE on the proper
  24. * order completion page to let UC Google Analytics know it should add the
  25. * e-commerce tracking code to the current page.
  26. *
  27. * The implementation below comes from the 2Checkout.com module which uses an
  28. * alternate checkout completion page.
  29. *
  30. * @return
  31. * TRUE if e-commerce tracking code should be added to the current page.
  32. */
  33. function hook_ucga_display() {
  34. // Tell UC Google Analytics to display the e-commerce JS on the custom
  35. // order completion page for this module.
  36. if (arg(0) == 'cart' && arg(1) == '2checkout' && arg(2) == 'complete') {
  37. return TRUE;
  38. }
  39. }
  40. /**
  41. * Allows modules to alter items passed to the e-commerce tracking code.
  42. *
  43. * The UC Google Analytics module constructs function calls that work through
  44. * the Google Analytics JS API to report purchased items for e-commerce tracking
  45. * purposes. The module builds the argument list for each product on an order
  46. * and uses this hook to give other modules a chance to alter what gets reported
  47. * to Google Analytics. Additional arguments passed to implementations of this
  48. * hook are provided for context.
  49. *
  50. * @param $item
  51. * An array of arguments being passed to Google Analytics representing an item
  52. * on the order, including order_id, sku, name, category, price, and qty.
  53. * @param $product
  54. * The product object as found in the $order object.
  55. * @param $trans
  56. * The array of arguments that were passed to Google Analytics to represent
  57. * the transaction.
  58. * @param $order
  59. * The order object being reported to Google Analytics.
  60. *
  61. * @return
  62. * Nothing should be returned. Hook implementations should receive the $item
  63. * array by reference and alter it directly.
  64. */
  65. function hook_ucga_item_alter(&$item, $product, $trans, $order) {
  66. // Example implementation: always set the category to "UBERCART".
  67. $item['category'] = 'UBERCART';
  68. }
  69. /**
  70. * Allows modules to alter the transaction data passed to Google Analytics.
  71. *
  72. * The UC Google Analytics module constructs function calls that work through
  73. * the Google Analytics JS API to report order information for e-commerce
  74. * tracking purposes. The module builds the argument list for the transaction
  75. * and uses this hook to give other modules a chance to alter what gets reported
  76. * to Google Analytics.
  77. *
  78. * @param $trans
  79. * An array of arguments being passed to Google Analytics representing the
  80. * transaction, including order_id, store, total, tax, shipping, city,
  81. * state, and country.
  82. * @param $order
  83. * The order object being reported to Google Analytics.
  84. *
  85. * @return
  86. * Nothing should be returned. Hook implementations should receive the $trans
  87. * array by reference and alter it directly.
  88. */
  89. function hook_ucga_trans_alter(&$trans, $order) {
  90. // Example implementation: prefix all orders with "UC-".
  91. $trans['order_id'] = 'UC-' . $trans['order_id'];
  92. }
  93. /**
  94. * @} End of "addtogroup hooks".
  95. */