Skip to content

How to integrate Hyrax's Google Analytics user stats into your Rails app

Michael J. Giarlo edited this page May 10, 2017 · 4 revisions

THIS DOCUMENT IS DEPRECATED (BECAUSE OUTDATED AND UNTESTED ON MODERN SUFIA)

See the Sufia Management Guide for more information on setting up Google Analytics.


Deprecated:

== Setup & Configuration

  • Run the generators $ rails g sufia:models:usagestats $ rails g sufia:models:cached_stats

  • Run the migrations $ rake db:migrate

  • Clear (or update) your existing cache

    This step is only necessary if you already have the file_view_stats and file_download_stats tables in your database (from the Sufia 4.2.0 release).

    The new migration added by the cached_stats generator will add a user_id column to the file_view_stats and file_download_stats tables. If you have existing data in those tables, those rows will be missing the user_id, which means that data won't be available for calculating the total number of file views and downloads for a user.

    There are 2 ways you can correct this problem:

    \1. Delete all the rows in the file_view_stats and file_download_stats tables, and then let the cache rebuild naturally.

    or:

    \2. Write a script that updates all the rows in the file_view_stats and file_download_stats tables to add the user_ids to the existing cache.

  • Edit config/initializers/sufia.rb

    • Set config.analytics = true
    • Set the config.analytic_start_date (Note: this was not necessary in Sufia 4.2.0, but it is needed going forward)
    • Set the config.google_analytics_id to your tracking ID (The tracking ID typically looks like UA-99999999-1)
  • Edit config/analytics.yml as described in the {README}[https://github.com/projecthydra/sufia/blob/master/README.md#analytics]

  • Run the rake task to build the cache in the user_stats table, and schedule it as a nightly background job

    • $ rake sufia:stats:user_stats
    • Schedule a nightly job to run that rake task so that user stats will update once a day
  • Update views

    If your Rails app overrides the app/views/dashboard/_index_partials/_stats.html.erb file from Sufia, then you may want to update it to display the user's combined file views & downloads stats.

Clone this wiki locally