Islandora usage stats

Track views of islandora items, show most popular items, and recent activity

Islandora Usage Stats

A simple module for Drupal 7 to track views of islandora items in the Ryerson Digital Repository.

Work based on code from https://github.com/roblib/islandora_scholar_upei and the scholar_tracking module for Drupal 6.

Features

Variables available:

How to use:

How to display tracking data on your template files:

An example:

We will add view and download stats to items in our Islandora PDF collection

Step 1

Navigate to your Drupal site's modules directory and clone the latest version of the module

$ cd <your_Drupal_Site>/sites/all/modules

$ git clone https://github.com/ryersonlibrary/islandora_usage_stats

Step 2

Open the islandora_usage_stats.module file in a text editor and create/edit the template preprocess function necessary to modify the Islandora PDF collection

$ vim islandora_usage_stats/islandora_usage_stats.module

The template preprocess function follows the naming convention of islandora_usage_stats_preprocess_MODULE_NAME(&$variables) so make sure to edit yours accordingly

function islandora_usage_stats_preprocess_islandora_pdf($&variables)
{
  $islandora_object = $variables['islandora_object'];
  $id = $islandora_object->id;
  $label = $islandora_object->label;

  $track = new IslandoraUsageStats($id);
  $variables['time_last_viewed'] = $track->getLastViewedTime();

  $currentStats = $track->getViewCount();
  $variables['times_viewed'] = $currentStats['views'];

  $downloaded = $track->getDownloads('OBJ');

  $variables['times_downloaded'] = $downloaded['downloads'];
  $variables['time_last_downloaded'] = $downloaded['time'];

  // we have added some bootstrap specific css classes and glyphicons to our link, include if desired
  $variables['islandora_download_link'] = l('<i class="glyphicon glyphicon-download"> ' . t('Download PDF') . '</i>', 'download_ds/' . $id . '/OBJ/' . $label, array(
    'attributes' => array(
          'class' => 'btn btn-primary'
      ),
      'html' => TRUE,
  ));
}

Step 3

Navigate to your site's themes directory and copy the Islandora PDF template file so we can make changes to it and override it

$ cd <your_Drupal_Site>/sites/themes/<yourtheme>

$ cp <your_Drupal_Site>/sites/all/islandora_solution_pack_pdf/theme/islandora-pdf.tpl.php .

Step 4

Edit the copied template file to output our new tracking variables

Before

/var/www/your_Drupal_Site/sites/themes/your_Theme/islandora-pdf.tpl.php

<!------- SNIP ---------->
<div class="islandora-pdf-content-wrapper clearfix">
<?php if (isset($islandora_content)): ?>

  <div class="islandora-pdf-content">
    <?php print $islandora_content; ?>
  </div>

  <?php if (isset($islandora_download_link)): ?>
  <div class="islandora-download-link">
    <?php print $islandora_download_link; ?>
  </div>
  <?php endif; ?>

<?php endif; ?>
</div>

After

/var/www/your_Drupal_Site/sites/themes/your_Theme/islandora-pdf.tpl.php

<!------- SNIP ---------->
<div class="islandora-pdf-content-wrapper clearfix">
<?php if (isset($islandora_content)): ?>

  <div class="islandora-pdf-content">
    <?php print $islandora_content; ?>
  </div>

  <?php if (isset($islandora_download_link)): ?>
  <div class="islandora-download-link">
    <?php print $islandora_download_link; ?>
  </div>
  <?php endif; ?>

<?php endif; ?>
</div>

<!-- usage stats -->
<?php if (module_exists('islandora_usage_stats')): ?>
<div class="islandora-usage-stats">
  <!-- we have added some bootstrap specific css classes (badge) to our view counts, include if desired -->
  <p>
    Viewed: <span class="badge"><?php print $times_viewed ?></span> Last viewed: <?php print $time_last_viewed==NULL ? 'Never' : date('g:ia, M d, Y', $time_last_viewed); ?>
  </p>
  <p>
    Downloaded: <span class="badge"><?php print $times_downloaded ?></span> Last downloaded: <?php print $time_last_downloaded==NULL ? 'Never' : date('g:ia, M d, Y', $time_last_downloaded); ?>
  </p>
</div>
<?php endif; ?>
<!-- end usage stats -->