You are here

function ad_track_event in Advertisement 7.3

Track an event, like a click or an impression.

Parameters

string $type: The type of the event; this is the bundle of the entity, so it needs to be defined as such.

int $nid: The ID of the ad node associated with the event.

string $parent_unique_id: (optional) The id of the parent event; e.g. impressions are parents of clicks. Defaults to ''.

string $url: (optional) The URL of the page. Defaults to NULL.

string $page_title: (optional) The title of the page. Defaults to NULL.

string $page_unique_id: (optional) A unique ID identifying a page view; all events (impressions and clicks) generated in a certain page view will have the same unique ID. Defaults to NULL.

string $event_unique_id: (optional) The unique id of the event; if NULL, a unique id will be generated. Defaults to NULL.

int $uid: (optional) The ID of the user associated with the event. If NULL, the current user will be used. Defaults to NULL.

Return value

int The unique ID of the newly created event.

2 calls to ad_track_event()
ad_click in ./ad.module
Callback during a click event.
ad_get_ads in ./ad.module
Callback for the Ajax request to get ads.

File

./ad.module, line 515
Core code for the ad module.

Code

function ad_track_event($type, $nid, $parent_unique_id = '', $url = NULL, $page_title = NULL, $page_unique_id = NULL, $event_unique_id = NULL, $uid = NULL) {
  if (is_null($event_unique_id)) {
    $event_unique_id = ad_uniqid($type);
  }
  if (is_null($uid)) {
    $uid = $GLOBALS['user']->uid;
  }
  $data = array(
    'type' => $type,
    'uid' => $uid,
    'created' => REQUEST_TIME,
    'ad' => $nid,
    'ip_address' => ip_address(),
    'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
    'url' => $url,
    // Drupal guarantees HTTP_REFERER is always populated (worst case it's
    // empty.
    'referrer' => $_SERVER['HTTP_REFERER'],
    'page_title' => $page_title,
    'page_unique_id' => $page_unique_id,
    'parent_unique_id' => $parent_unique_id,
    'session' => ad_session_get(),
    'unique_id' => $event_unique_id,
  );
  $queue_info = variable_get('ad_queue_info', []);
  if (!empty($queue_info[$type])) {
    $queue = DrupalQueue::get('ad');

    // Store the impression into a queue for later processing.
    $queue
      ->createItem($data);
  }
  else {
    ad_bootstrap_full();
    $event = entity_create('tracked_event', $data);
    $event
      ->save();
    if (in_array($type, array(
      'click',
      'impression',
    ))) {
      $counter = $type == 'click' ? 'total_clicks' : 'total_impressions';
      ad_increase_denormalized_counter($nid, $counter);
    }
  }

  // Another implementation might return UUIDs, if that's faster.
  return $event_unique_id;
}