FINAL suepr merge step : added all modules to this super repos
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Hooks provided by the Google Analytics for Ubercart module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @addtogroup hooks
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Determines whether e-commerce tracking code should be added to the page.
|
||||
*
|
||||
* The Google Analytics module takes care of adding the necessary .js file from
|
||||
* Google for tracking general statistics. The UC Google Analytics module works
|
||||
* in conjunction with this code to add e-commerce specific code. However, the
|
||||
* e-commerce code should only be added on appropriate pages. Generally, the
|
||||
* correct page will be the checkout completion page at cart/checkout/complete.
|
||||
* However, because modules can change the checkout flow as necessary, it must
|
||||
* be possible for alternate pages to be used.
|
||||
*
|
||||
* This hook allows other modules to tell the UC Google Analytics module that
|
||||
* it should go ahead and add the e-commerce tracking code to the current page.
|
||||
* A module simply needs to implement this hook and return TRUE on the proper
|
||||
* order completion page to let UC Google Analytics know it should add the
|
||||
* e-commerce tracking code to the current page.
|
||||
*
|
||||
* The implementation below comes from the 2Checkout.com module which uses an
|
||||
* alternate checkout completion page.
|
||||
*
|
||||
* @return
|
||||
* TRUE if e-commerce tracking code should be added to the current page.
|
||||
*/
|
||||
function hook_ucga_display() {
|
||||
// Tell UC Google Analytics to display the e-commerce JS on the custom
|
||||
// order completion page for this module.
|
||||
if (arg(0) == 'cart' && arg(1) == '2checkout' && arg(2) == 'complete') {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows modules to alter items passed to the e-commerce tracking code.
|
||||
*
|
||||
* The UC Google Analytics module constructs function calls that work through
|
||||
* the Google Analytics JS API to report purchased items for e-commerce tracking
|
||||
* purposes. The module builds the argument list for each product on an order
|
||||
* and uses this hook to give other modules a chance to alter what gets reported
|
||||
* to Google Analytics. Additional arguments passed to implementations of this
|
||||
* hook are provided for context.
|
||||
*
|
||||
* @param $item
|
||||
* An array of arguments being passed to Google Analytics representing an item
|
||||
* on the order, including order_id, sku, name, category, price, and qty.
|
||||
* @param $product
|
||||
* The product object as found in the $order object.
|
||||
* @param $trans
|
||||
* The array of arguments that were passed to Google Analytics to represent
|
||||
* the transaction.
|
||||
* @param $order
|
||||
* The order object being reported to Google Analytics.
|
||||
*
|
||||
* @return
|
||||
* Nothing should be returned. Hook implementations should receive the $item
|
||||
* array by reference and alter it directly.
|
||||
*/
|
||||
function hook_ucga_item_alter(&$item, $product, $trans, $order) {
|
||||
// Example implementation: always set the category to "UBERCART".
|
||||
$item['category'] = 'UBERCART';
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows modules to alter the transaction data passed to Google Analytics.
|
||||
*
|
||||
* The UC Google Analytics module constructs function calls that work through
|
||||
* the Google Analytics JS API to report order information for e-commerce
|
||||
* tracking purposes. The module builds the argument list for the transaction
|
||||
* and uses this hook to give other modules a chance to alter what gets reported
|
||||
* to Google Analytics.
|
||||
*
|
||||
* @param $trans
|
||||
* An array of arguments being passed to Google Analytics representing the
|
||||
* transaction, including order_id, store, total, tax, shipping, city,
|
||||
* state, and country.
|
||||
* @param $order
|
||||
* The order object being reported to Google Analytics.
|
||||
*
|
||||
* @return
|
||||
* Nothing should be returned. Hook implementations should receive the $trans
|
||||
* array by reference and alter it directly.
|
||||
*/
|
||||
function hook_ucga_trans_alter(&$trans, $order) {
|
||||
// Example implementation: prefix all orders with "UC-".
|
||||
$trans['order_id'] = 'UC-' . $trans['order_id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @} End of "addtogroup hooks".
|
||||
*/
|
@@ -0,0 +1,15 @@
|
||||
name = Google Analytics for Ubercart
|
||||
description = Adds e-commerce tracking to the Google Analytics module.
|
||||
dependencies[] = googleanalytics
|
||||
dependencies[] = uc_cart
|
||||
dependencies[] = uc_order
|
||||
dependencies[] = uc_store
|
||||
package = Ubercart - extra
|
||||
core = 7.x
|
||||
|
||||
; Information added by Drupal.org packaging script on 2013-12-17
|
||||
version = "7.x-3.6"
|
||||
core = "7.x"
|
||||
project = "ubercart"
|
||||
datestamp = "1387304010"
|
||||
|
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Adds the required Javascript to the checkout completion page to allow
|
||||
* e-commerce statistics tracking through Google Analytics.
|
||||
*
|
||||
* Refer to http://code.google.com/apis/analytics/docs/gaTrackingEcommerce.html
|
||||
* for documentation on the functions used to submit e-commerce statistics to
|
||||
* Google Analytics.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_enable().
|
||||
*/
|
||||
function uc_googleanalytics_enable() {
|
||||
// Get the weight of the Google Analytics module.
|
||||
$weight = db_query("SELECT weight FROM {system} WHERE name = :module", array(':module' => 'googleanalytics'))->fetchField();
|
||||
|
||||
// Update the weight of the UC Google Analytics module so its hooks get called
|
||||
// after the actual Google Analytics module.
|
||||
db_update('system')
|
||||
->fields(array('weight' => max(1000, $weight + 1)))
|
||||
->condition('name', 'uc_googleanalytics')
|
||||
->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_page_alter().
|
||||
*/
|
||||
function uc_googleanalytics_page_alter(&$page) {
|
||||
// Check to see if we are at the order completion page.
|
||||
if (uc_googleanalytics_display()) {
|
||||
// If so, then if we can load the order...
|
||||
if (!empty($_SESSION['ucga_order_id']) && $order = uc_order_load($_SESSION['ucga_order_id'])) {
|
||||
// Build the GA tracking code.
|
||||
$script = uc_googleanalytics_ecommerce_js($order);
|
||||
|
||||
// Add the code to the footer.
|
||||
drupal_add_js($script, array('type' => 'inline', 'scope' => 'footer', 'preprocess' => FALSE));
|
||||
}
|
||||
|
||||
// Clean out the session variable.
|
||||
if (isset($_SESSION['ucga_order_id'])) {
|
||||
unset($_SESSION['ucga_order_id']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_uc_order().
|
||||
*/
|
||||
function uc_googleanalytics_uc_order($op, $order, $arg2) {
|
||||
// If a new order is created during the checkout process...
|
||||
if ($op == 'new') {
|
||||
// Store the order ID for later use.
|
||||
$_SESSION['ucga_order_id'] = $order->order_id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether or not to display the e-commerce related JS through GA.
|
||||
*
|
||||
* @return
|
||||
* TRUE or FALSE indicating whether or not to display the GA e-commerce JS.
|
||||
*/
|
||||
function uc_googleanalytics_display() {
|
||||
// Display the GA e-commerce JS if the URL is cart/checkout/complete...
|
||||
if (arg(0) == 'cart' && arg(1) == 'checkout' && arg(2) == 'complete') {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Or if the URL is the custom completion page.
|
||||
$completion_page = variable_get('uc_cart_checkout_complete_page', '');
|
||||
|
||||
if (!empty($completion_page) && $completion_page == drupal_get_path_alias($_GET['q'])) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Or if another module says this is the page through hook_ucga_display().
|
||||
foreach (module_invoke_all('ucga_display') as $result) {
|
||||
if ($result === TRUE) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise return FALSE.
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the e-commerce JS passed to Google Analytics for order tracking.
|
||||
*
|
||||
* @param $order
|
||||
* The fully loaded order object to convert into GA JS.
|
||||
*
|
||||
* @return
|
||||
* The JS that should be added to the page footer.
|
||||
*/
|
||||
function uc_googleanalytics_ecommerce_js($order) {
|
||||
$script = '';
|
||||
|
||||
// Lookup the name of the country or default to the ID if it can't be found
|
||||
// for some reason.
|
||||
if ($country_data = uc_get_country_data(array('country_id' => $order->billing_country))) {
|
||||
$order->billing_country_name = $country_data[0]['country_name'];
|
||||
}
|
||||
else {
|
||||
$order->billing_country_name = $order->billing_country;
|
||||
}
|
||||
|
||||
// Lookup the name of the zone.
|
||||
$order->billing_zone_name = uc_zone_get_by_id($order->billing_zone);
|
||||
|
||||
// Calculate order tax and shipping totals.
|
||||
$order->tax_total = 0;
|
||||
$order->shipping_total = 0;
|
||||
|
||||
foreach ($order->line_items as $line_item) {
|
||||
if ($line_item['type'] == 'tax') {
|
||||
$order->tax_total += $line_item['amount'];
|
||||
}
|
||||
elseif ($line_item['type'] == 'shipping') {
|
||||
$order->shipping_total += $line_item['amount'];
|
||||
}
|
||||
}
|
||||
|
||||
// Build the transaction arguments.
|
||||
$trans = array(
|
||||
'order_id' => $order->order_id,
|
||||
'store' => uc_store_name(),
|
||||
'total' => $order->order_total,
|
||||
'tax' => $order->tax_total,
|
||||
'shipping' => $order->shipping_total,
|
||||
'city' => $order->billing_city,
|
||||
'state' => $order->billing_zone_name,
|
||||
'country' => $order->billing_country_name,
|
||||
);
|
||||
|
||||
// Allow modules to alter the transaction arguments.
|
||||
drupal_alter('ucga_trans', $trans, $order);
|
||||
|
||||
// Put the arguments into an array that is safe to implode directly.
|
||||
$args = array(
|
||||
'"' . $trans['order_id'] . '"',
|
||||
drupal_json_encode($trans['store']),
|
||||
'"' . $trans['total'] . '"',
|
||||
'"' . $trans['tax'] . '"',
|
||||
'"' . $trans['shipping'] . '"',
|
||||
drupal_json_encode($trans['city']),
|
||||
drupal_json_encode($trans['state']),
|
||||
drupal_json_encode($trans['country']),
|
||||
);
|
||||
|
||||
// Add the transaction line to the JS.
|
||||
$script .= '_gaq.push(["_addTrans", ' . implode(', ', $args) . ']);';
|
||||
|
||||
// Loop through the products on the order.
|
||||
foreach ($order->products as $product) {
|
||||
$product->category = '';
|
||||
|
||||
// Try to find a category (term) for the product. Since products most often
|
||||
// only have one category, the first one returned (in the
|
||||
// $node->taxonomy_catalog) is chosen.
|
||||
if (module_exists('taxonomy')) {
|
||||
$node = node_load($product->nid);
|
||||
if (isset($node->taxonomy_catalog[LANGUAGE_NONE][0]['tid'])) {
|
||||
$term = taxonomy_term_load($node->taxonomy_catalog[LANGUAGE_NONE][0]['tid']);
|
||||
$product->category = $term->name;
|
||||
}
|
||||
}
|
||||
if (empty($product->category)) {
|
||||
$product->category = t('No category');
|
||||
}
|
||||
|
||||
// Build the item arguments.
|
||||
$item = array(
|
||||
'order_id' => $order->order_id,
|
||||
'sku' => $product->model,
|
||||
'name' => $product->title,
|
||||
'category' => $product->category,
|
||||
'price' => $product->price,
|
||||
'qty' => $product->qty,
|
||||
);
|
||||
|
||||
// Allow modules to alter the item arguments.
|
||||
drupal_alter('ucga_item', $item, $product, $trans, $order);
|
||||
|
||||
// Put the arguments into an array that is safe to implode directly.
|
||||
$args = array(
|
||||
'"' . $item['order_id'] . '"',
|
||||
drupal_json_encode($item['sku']),
|
||||
drupal_json_encode($item['name']),
|
||||
drupal_json_encode((string) $item['category']),
|
||||
'"' . $item['price'] . '"',
|
||||
'"' . $item['qty'] . '"',
|
||||
);
|
||||
|
||||
// Add the item line to the JS.
|
||||
$script .= '_gaq.push(["_addItem", ' . implode(', ', $args) . ']);';
|
||||
}
|
||||
|
||||
// Add the function to submit the transaction to GA.
|
||||
$script .= '_gaq.push(["_trackTrans"]);';
|
||||
|
||||
return $script;
|
||||
}
|
Reference in New Issue
Block a user