is would be great to define your own period for a field. Like "Last 2 days".

Comments

patrickd’s picture

thanks for your request,

unfortunately this is a little complicated as piwik either wants the period to be a string like "day", "week", "month", "year" or it wants to have a specific date "range". That means that a string like "last 2 days" must be converted into a date range. (See piwik API documentation for reference).

To make a good implementation of this we need to find an elegant way to transform thresholds into date ranges

Erik Seifert’s picture

How about make a field config ?

Period: custom
Range: 22.12.2011 - 24.12.2011

patrickd’s picture

yes that would be easily possible, but that would not make possible to define it like "last two days" (unless you edit the fields config every day). I already thought about just making this range setting available but I couldn't think of any usecase worth the effort. (why does somebody just want static node statistics from a date range?)

Erik Seifert’s picture

Example ;-)

Now day period show hits for only this day 00:00 - 24:00 . But we need the last 24 Hours ;- ) So if you look on the website at 00:01 , no entry appears.

patrickd’s picture

yes but custom ranges won't help in your example

Period: custom
Range: 22.12.2011 - 24.12.2011
now you have to change the date every day manually, otherwise it will show statistics from 22.12.2011 - 24.12.2011

What you need is a custom dynamic range like
"last two days"
and this must automatically be translated into
[current date - 2days] until [current date]
before submitting it to piwik api

this is the problem I meant with - how to solve this in an elegant way

Erik Seifert’s picture

Give me some days. I will think it over ;- )

Romka’s picture

What do you think about next solution?

First, I propose to add new parameters to widget settings ('range-2' and 'range-3' in this example):

function piwik_stats_field_settings_form($field, $instance, $has_data) {
  $settings = $field['settings'];
  // Add field settings for statistic period.
  $form['period'] = array(
    '#type' => 'select',
    '#title' => t('Period'),
    '#default_value' => $settings['period'],
    '#options' => array(
      'day' => t('Day'),
      'range-2' => t('2 days'),
      'range-3' => t('3 days'),
      'week' => t('Week'),
      'month' => t('Month'),
      'year' => t('Year'),
    ),
    '#required' => TRUE,
    '#description' => t('The period of the requested statistics.'),
  );
  return $form;
}

Then add next code to the function piwik_stats_api_request:

if (strstr($period, 'range-')) {
  $range = str_replace('range-', '', $period);
  $date = date('Y-m-d', time() - 86400 * $period) . ',' . date('Y-m-d');

  $period = 'range';
}

In result this function would look like this:

function piwik_stats_api_request($piwik_url, $token_auth, $method, $site_id, $period = 'year', $date = 'now', $format = 'xml') {
  if (strstr($period, 'range-')) {
    $range = str_replace('range-', '', $period);
    $date = date('Y-m-d', time() - 86400 * $period) . ',' . date('Y-m-d');

    $period = 'range';
  }

  // Send off the API request.
  return drupal_http_request(
    url($piwik_url,
      array(
        'query' => array(
          'module' => 'API',
          'method' => $method,
          'idSite' => $site_id,
          'period' => $period,
          'date' => $date,
          'format' => $format,
          'token_auth' => $token_auth,
          'expanded' => TRUE,
        ),
      )
    )
  );
}
patrickd’s picture

would be a way, but this means all ranges are hardcoded and only the ranges available in the select box can be chosen,

I'd rather like to have a "Range" item in the period select and if you select range another textfield apears where you can enter a range like "+2 days", "+6 hours", etc.
This string can then be translated into a range of the "current_time" until "strtotime(range, current_time)".

$date = date('Y-m-d', strtotime($range)) . ',' . date('Y-m-d');

$form['period'] = array(
    '#type' => 'select',
    '#title' => t('Period'),
    '#default_value' => $settings['period'],
    '#options' => array(
      'day' => t('Day'),
      'week' => t('Week'),
      'month' => t('Month'),
      'year' => t('Year'),
      'range' => t('Custom range'),
    ),
  );
Erik Seifert’s picture

patrickd’s picture

marked #2058935: Statistics field settings 'Period' as duplicate of this feature request.

phem’s picture

Version: 7.x-2.0-beta1 » 7.x-2.0-beta3

this is my solution:

//$X number of days to sum the reports up.
$period = 'range';
$date = 'previous' . $X;

See http://piwik.org/docs/analytics-api/reference/ for explanation of lastX and previousX