<commit_before>module AnalyticsPresenter
  extend ActiveSupport::Concern

  def pageviews_by_path(path)
    count = 0
    profile = google_analytics_profile
    if profile.present?
      begin
        Pageview.results(profile).each do |entry|
          count += entry[:pageviews].to_i if entry[:pagePath] == path
        end
      rescue OAuth2::Error => e
        Rails.logger.error(e.code["message"])
      end
    end
    return count
  end

  def pageviews_by_ids(ids)
    count = 0
    profile = google_analytics_profile
    if profile.present?
      begin
        Pageview.results(profile).each do |entry|
          ids.each do |id|
            count += entry[:pageviews].to_i if entry[:pagePath].include? id
          end
        end
      rescue OAuth2::Error => e
        # TODO: we're hitting GA quotas for monograph_catalog pages in production.
        # Need to figure out a better way to do this...
        Rails.logger.error(e.code["message"])
        return nil
      end
    end
    return count
  end

  def google_analytics_id
    press = Press.find_by(subdomain: subdomain)
    return press.google_analytics unless press.nil? || press.google_analytics.nil?
    Rails.application.secrets.google_analytics_id
  end

  def google_analytics_profile
    profile = AnalyticsService.profile(google_analytics_id)
    unless profile
      Rails.logger.error("Google Analytics profile has not been established. Unable to fetch statistics.")
      return []
    end
    return profile
  end
end<commit_msg>hit the ga quota in a different way, expanding the exception handling<commit_after>module AnalyticsPresenter
  extend ActiveSupport::Concern

  def pageviews_by_path(path)
    count = 0
    begin
      profile = google_analytics_profile
      if profile.present?
        Pageview.results(profile).each do |entry|
          count += entry[:pageviews].to_i if entry[:pagePath] == path
        end
      end
    rescue OAuth2::Error => e
      Rails.logger.error(e.code["message"])
    end
    return count
  end

  def pageviews_by_ids(ids)
    count = 0
    begin
      profile = google_analytics_profile
      if profile.present?
        Pageview.results(profile).each do |entry|
          ids.each do |id|
            count += entry[:pageviews].to_i if entry[:pagePath].include? id
          end
        end
      end
    rescue OAuth2::Error => e
      # TODO: we're hitting GA quotas for monograph_catalog pages in production.
      # Need to figure out a better way to do this...
      Rails.logger.error(e.code["message"])
      return nil
    end
    return count
  end

  def google_analytics_id
    press = Press.find_by(subdomain: subdomain)
    return press.google_analytics unless press.nil? || press.google_analytics.nil?
    Rails.application.secrets.google_analytics_id
  end

  def google_analytics_profile
    profile = AnalyticsService.profile(google_analytics_id)
    unless profile
      Rails.logger.error("Google Analytics profile has not been established. Unable to fetch statistics.")
      return []
    end
    return profile
  end
end
{'fraction_non_alphanumeric': 0.0770982433311646, 'fraction_numerical': 0.002602472348731295, 'mean_word_length': 2.6694510739856803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2003666', 'n_tokens_mistral': 900, 'n_tokens_neox': 876, 'n_words': 248}