You are here

function piwik_stats_request_data in Piwik Statistic Integration 7

Requests and refreshes the Piwik Statistical data.

Parameters

string $token_auth: Authentication token needed to authenticate with piwik.

int $site_id: Unique site ID of piwik.

string $piwik_url: URL to piwik.

string $period: Statistical period.

2 calls to piwik_stats_request_data()
piwik_stats_cron in ./piwik_stats.module
Implements hook_cron().
piwik_stats_piwik_admin_settings_request_statistical_data_submit in ./piwik_stats.module
Submit callback for refreshing piwik_stats data.

File

./piwik_stats.module, line 245
Integrates piwik statistics per node.

Code

function piwik_stats_request_data($token_auth, $site_id, $piwik_url, $period) {
  if (empty($token_auth) || empty($site_id) || empty($piwik_url)) {
    watchdog('piwik_stats', 'Requesting Piwik Statistics failed: Authentication token, site ID and HTTP URL are required', array(), WATCHDOG_ERROR);
    return;
  }

  // Request piwik XML data.
  $result = piwik_stats_api_request($piwik_url, $token_auth, 'Actions.getPageUrls', $site_id, $period);
  if ($result->code == !200) {
    watchdog('piwik_stats', 'Requesting Piwik Statistics failed: HTTP returned: @code.', array(
      '@code' => $result->code,
    ), WATCHDOG_ERROR);
    return;
  }

  // Parse XML data.
  $xml = new SimpleXMLElement($result->data);
  if (empty($xml)) {
    watchdog('piwik_stats', 'Requesting Piwik Statistics failed: Could not parse XML.', array(), WATCHDOG_ERROR);
    return;
  }

  // Flush piwik statistic table.
  db_truncate('piwik_stats')
    ->execute();

  // Get all nodes.
  $select = db_select('node', 'n');
  $select
    ->addField('n', 'nid');
  $nodes = $select
    ->execute()
    ->fetchAll();
  foreach ($nodes as $node) {
    $urls = array();

    // Assemble default absolute node URL.
    $urls[] = url('node/' . $node->nid, array(
      'absolute' => TRUE,
      'alias' => TRUE,
    ));

    // Get absulute alias URLs.
    $select = db_select('url_alias', 'u');
    $select
      ->addField('u', 'alias');
    $select
      ->condition('u.source', 'node/' . $node->nid);
    $aliases = $select
      ->execute()
      ->fetchAll();
    foreach ($aliases as $alias) {
      $urls[] = url($alias->alias, array(
        'absolute' => TRUE,
        'alias' => TRUE,
      ));
    }

    // Create a default statistic array for db_insert.
    $stats = array(
      'nid' => $node->nid,
      'nb_visits' => 0,
      'nb_hits' => 0,
      'entry_nb_visits' => 0,
      'entry_nb_actions' => 0,
      'entry_sum_visit_length' => 0,
      'entry_bounce_count' => 0,
      'exit_nb_visits' => 0,
      'sum_time_spent' => 0,
      'avg_time_on_page' => 0,
      'bounce_rate' => 0,
      'exit_rate' => 0,
    );

    // Sum up statistics per url and write them into the statistic array.
    $count = 0;
    foreach ($urls as $url) {
      $url_stats = $xml
        ->xpath('//url[text()="' . $url . '"]/..');
      if (!empty($url_stats)) {
        $count++;

        // The following values are integers and can be summed up easily.
        if (isset($url_stats[0]->nb_visits)) {
          $stats['nb_visits'] += $url_stats[0]->nb_visits;
        }
        if (isset($url_stats[0]->nb_hits)) {
          $stats['nb_hits'] += $url_stats[0]->nb_hits;
        }
        if (isset($url_stats[0]->entry_nb_visits)) {
          $stats['entry_nb_visits'] += $url_stats[0]->entry_nb_visits;
        }
        if (isset($url_stats[0]->entry_nb_actions)) {
          $stats['entry_nb_actions'] += $url_stats[0]->entry_nb_actions;
        }
        if (isset($url_stats[0]->entry_sum_visit_length)) {
          $stats['entry_sum_visit_length'] += $url_stats[0]->entry_sum_visit_length;
        }
        if (isset($url_stats[0]->entry_bounce_count)) {
          $stats['entry_bounce_count'] += $url_stats[0]->entry_bounce_count;
        }
        if (isset($url_stats[0]->exit_nb_visits)) {
          $stats['exit_nb_visits'] += $url_stats[0]->exit_nb_visits;
        }
        if (isset($url_stats[0]->sum_time_spent)) {
          $stats['sum_time_spent'] += $url_stats[0]->sum_time_spent;
        }

        // This is an average value. It needs to be divided by count later.
        if (isset($url_stats[0]->avg_time_on_page)) {
          $stats['avg_time_on_page'] += $url_stats[0]->avg_time_on_page;
        }

        // These two are percent values (##% formatted).
        // We need to transform them to integers before we can sum them up.
        if (isset($url_stats[0]->bounce_rate)) {
          $stats['bounce_rate'] += (int) drupal_substr($url_stats[0]->bounce_rate, 0, -1);
        }
        if (isset($url_stats[0]->exit_rate)) {
          $stats['exit_rate'] += (int) drupal_substr($url_stats[0]->exit_rate, 0, -1);
        }
      }
    }

    // Divide it, to get a real average value.
    if (isset($url_stats[0]->sum_time_spent)) {
      $stats['avg_time_on_page'] = (int) ($stats['avg_time_on_page'] / $count);
    }

    // Insert statistical data.
    db_insert('piwik_stats')
      ->fields($stats)
      ->execute();
  }

  // Clean up the huge xml stuff.
  unset($result, $xml);
  watchdog('piwik_stats', 'Piwik Statistical data was requested and refreshed successfully.');
  return TRUE;
}