/[drupal]/contributions/modules/ubercart/uc_googleanalytics/uc_googleanalytics.module
ViewVC logotype

Contents of /contributions/modules/ubercart/uc_googleanalytics/uc_googleanalytics.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Thu Jul 10 12:41:02 2008 UTC (16 months, 2 weeks ago) by islandusurper
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--2
Changes since 1.5: +37 -27 lines
File MIME type: text/x-php
Begin the Ubercart 6.x-2.x branch.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Google Analytics e-commerce tracking
7 *
8 * by Erlend Stromsvik : erlend@nymedia.no / Quaoar at ubercart.org
9 *
10 * Adds the required HTML and Javascript to the checkout complete page to allow
11 * e-commerce tracking through Google Analytics.
12 *
13 * E-commerce tracking works by hooking onto the hook_order and for $op == "new"
14 * setting a session variable "uc_googleanalytics_order_id" with the order id.
15 * The implementation of hook_footer will check for the path
16 * "cart/checkout/complete". When that path is requested the registered order
17 * will be loaded from the sessions variable and necessary data will be output
18 * for Google Analytics.
19 */
20
21 /*******************************************************************************
22 * Hook Functions
23 ******************************************************************************/
24
25 /**
26 * Implementation of hook_footer() to insert the required HTML and Javascript.
27 */
28 function uc_googleanalytics_footer($main = 0) {
29 $checkout_page = variable_get('uc_cart_checkout_complete_page', '');
30
31 // check to see if we are at the order complete page
32 if ((arg(0) == 'cart' && arg(1) == 'checkout' && arg(2) == 'complete') ||
33 !empty($checkout_page) && $checkout_page == $_GET['q']) {
34 if ($order = uc_order_load($_SESSION['uc_googleanalytics_order_id'])) {
35 $script .= _uc_googleanalytics_ecommerce_form($order);
36 $_SESSION['uc_googleanalytics_order_id'] = NULL;
37 unset($_SESSION['uc_googleanalytics_order_id']);
38 uc_add_js('$(document).ready(function() { __utmSetTrans(); });', 'inline');
39 }
40 }
41 return $script;
42 }
43
44
45 /*******************************************************************************
46 * Hook Functions (Ubercart)
47 ******************************************************************************/
48
49 function uc_googleanalytics_order($op, &$arg1, $arg2) {
50 switch ($op) {
51 case 'new':
52 if (variable_get('uc_googleanalytics_commercetracking', TRUE)) {
53 // store the order id for later use. We won't be able to pick up the order id at cart/checkout/complete
54 $_SESSION['uc_googleanalytics_order_id'] = $arg1->order_id;
55 }
56 break;
57 }
58 }
59
60
61 /*******************************************************************************
62 * Helper Functions
63 ******************************************************************************/
64
65 function _uc_googleanalytics_ecommerce_form(&$order) {
66 $script = '';
67 $UTM_T = '';
68 $UTM_I = '';
69 $tax = 0;
70 $shipping_cost = 0;
71
72 // Finding name of country, if not use the country code from ubercart
73 if ($country_data = uc_get_country_data(array("country_id" => $order->billing_country))) {
74 $country = $country_data[0]['country_name'];
75 } else {
76 $country = $order->billing_country;
77 }
78
79 foreach ($order->products as $product) {
80 $category = '';
81
82 // Try to find a category (term) for the product. Since products most often
83 // only have one category, the first one returned (based on tid) is chosen.
84 if (module_exists('taxonomy')) {
85 $terms = taxonomy_node_get_terms($product->nid);
86 if (count($terms)) {
87 $term = array_shift($terms);
88 $category = $term->name;
89 }
90 }
91 if (empty($category)) {
92 $category = t('No category');
93 }
94
95 // using the model field as SKU
96 $UTM_I .= 'UTM:I|' . $product->order_id .'|'. $product->model .'|'
97 . $product->title .'|'. $category .'|'. $product->price .'|'
98 . $product->qty ." \n";
99 }
100
101 foreach ($order->line_items as $line_item) {
102 if ($line_item['type'] == 'tax') {
103 $tax += $line_item['amount'];
104 }
105 if ($line_item['type'] == 'shipping') {
106 $shipping_cost += $line_item['amount'];
107 }
108 }
109
110 $UTM_T = 'UTM:T|' . $order->order_id
111 .'|' . variable_get('uc_store_name', 'Ubercart')
112 .'|' . $order->order_total
113 .'|' . $tax
114 .'|' . $shipping_cost
115 .'|' . check_plain($order->billing_city)
116 .'|' . check_plain($order->billing_postal_code)
117 .'|' . $country . " \n";
118
119 $script .= "<form style=\"display:none;\" name=\"utmform\">\n";
120 $script .= "<textarea id=\"utmtrans\">\n";
121 $script .= $UTM_T;
122 $script .= $UTM_I;
123 $script .= "</textarea>\n";
124 $script .= "</form>\n";
125
126 return $script;
127 }

  ViewVC Help
Powered by ViewVC 1.1.2