You are here

function ga_push_add in GA Push 8

Same name and namespace in other branches
  1. 7 ga_push.module \ga_push_add()

Add a new google analytics tracking push.

The behavior of this function depends on the parameters it is called with. The following actions can be performed using this function:

  • Event tracking
  • Ecommerce tracking
  • Social tracking
  • Pageview tracking
  • ...

For better documentation understanding this function has been splited in to several functions and should not be called directly.

Parameters

array $push_params:

array $options: (optional) An associative array of additional options, with the following elements:

Only for server-side methods:

  • 'tid': Override Tracking ID / Web Property ID (Classic User account aka UA) of the google analytics module config. Universal and Classic GA Methods.
  • 'cid': Override Universal Client ID of the current user cookie. Only for Universal GA methods.
  • 'utma': Force Classic GA __utma cookie of the current user cookie Only for Classic GA methods.
  • 'utmb': Force Classic GA __utmb cookie of the current user cookie Only for Classic GA methods.
  • 'ua': Override User Agent of the current user. Only for Universal GA methods.
  • 'uip': Override User IP of the current user. Only for Universal GA methods.

All methods:

  • 'context': An array of aditional info to be passed to drupal_alter implementations.

See also

https://developers.google.com/analytics/devguides/collection/gajs/

https://developers.google.com/analytics/devguides/collection/analyticsjs/

ga_push_add_ecommerce()

ga_push_add_event()

ga_push_add_pageview()

ga_push_add_social()

5 calls to ga_push_add()
ga_push_add_ecommerce in ./ga_push.module
Push a GA ecommerce.
ga_push_add_event in ./ga_push.module
Push a GA event.
ga_push_add_exception in ./ga_push.module
Push a GA exception.
ga_push_add_pageview in ./ga_push.module
Push a GA pageview.
ga_push_add_social in ./ga_push.module
Push a GA social.

File

./ga_push.module, line 162
Drupal Module: GA Push.

Code

function ga_push_add(array $push_params, $type = GA_PUSH_TYPE_EVENT, $method_key = NULL, array $options = []) {
  global $user;
  $push = [
    'type' => $type,
    'method_key' => $method_key,
    'options' => $options,
  ];

  // Add push parameters using legacy compatibility:
  // Map old method keys to the new ones.
  // @NOTE: this feature will be remove in future realeases.
  $push['params'] = ga_push_add_legacy_params($push_params, $push['type']);

  // Let other modules alter the current push data:
  Drupal::moduleHandler()
    ->alter('ga_push_add', $push);
  if (is_array($push['params']) && count($push['params'])) {

    // @TODO Check for _googleanalytics_visibility_user to work and Remove TRUE
    if (TRUE || _googleanalytics_visibility_user($user)) {

      // If the select method is not available or is not defined:
      if (is_null($push['method_key']) || $push['method_key'] == GA_PUSH_METHOD_DEFAULT || !is_null($push['method_key']) && !ga_push_method_available($push['method_key'], $push['type'])) {
        $push['method_key'] = \Drupal::config('ga_push.settings')
          ->get('default_method');
      }
      if (!is_null($push['method_key'])) {
        $method = ga_push_get_method($push['method_key']);
        if (isset($method['file'])) {
          require_once $method['file'];
        }

        // @TODO: remove deprecated!
        switch ($push['type']) {
          case GA_PUSH_TYPE_EVENT:

            // If the VALUE argument is not numeric -> 1.
            $push['params']['eventValue'] = isset($push['params']['eventValue']) && is_numeric($push['params']['eventValue']) ? $push['params']['eventValue'] : 1;
            break;
        }
        call_user_func($method['callback'], $push['params'], $push['type'], $push['options']);
      }
      else {

        // @TODO: watchdog there's no method available.
      }
    }
  }
}