Class: CypressViewportUpdater::GoogleAnalyticsReports

Inherits:
Object
  • Object
show all
Includes:
Google::Apis::AnalyticsreportingV4, Google::Auth, SentryLogging
Defined in:
app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb

Constant Summary collapse

JSON_CREDENTIALS =
Settings.google_analytics_cvu.to_json
SCOPE =
'https://www.googleapis.com/auth/analytics.readonly'
VIEW_ID =
'176188361'

Instance Method Summary collapse

Methods included from SentryLogging

#log_exception_to_sentry, #log_message_to_sentry, #non_nil_hash?, #normalize_level, #rails_logger, #set_sentry_metadata

Constructor Details

#initializeGoogleAnalyticsReports

Returns a new instance of GoogleAnalyticsReports.



15
16
17
18
19
20
21
22
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 15

def initialize
  @analytics = AnalyticsReportingService.new
  @analytics.authorization = ServiceAccountCredentials.make_creds(
    json_key_io: StringIO.new(JSON_CREDENTIALS),
    scope: SCOPE
  )
  @reports = nil
end

Instance Method Details

#date_rangeObject (private)



67
68
69
70
71
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 67

def date_range
  start_date = CypressViewportUpdater::UpdateCypressViewportsJob::START_DATE
  end_date = CypressViewportUpdater::UpdateCypressViewportsJob::END_DATE
  DateRange.new(start_date:, end_date:)
end

#dimension_device_categoryObject (private)



77
78
79
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 77

def dimension_device_category
  Dimension.new(name: 'ga:deviceCategory')
end

#dimension_screen_resolutionObject (private)



81
82
83
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 81

def dimension_screen_resolution
  Dimension.new(name: 'ga:screenResolution')
end

#metric_userObject (private)



73
74
75
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 73

def metric_user
  Metric.new(expression: 'ga:users')
end

#request_reportsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 24

def request_reports
  request = GetReportsRequest.new(report_requests: [
                                    user_report_request,
                                    viewport_report_request
                                  ])
  begin
    @reports = @analytics.batch_get_reports(request).reports
  rescue => e
    log_exception_to_sentry(e)
  end

  self
end

#user_reportObject



38
39
40
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 38

def user_report
  @reports[0] if @reports && @reports[0]
end

#user_report_requestObject (private)



48
49
50
51
52
53
54
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 48

def user_report_request
  ReportRequest.new(
    view_id: VIEW_ID,
    date_ranges: [date_range],
    metrics: [metric_user]
  )
end

#viewport_reportObject



42
43
44
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 42

def viewport_report
  @reports[1] if @reports && @reports[1]
end

#viewport_report_requestObject (private)



56
57
58
59
60
61
62
63
64
65
# File 'app/sidekiq/cypress_viewport_updater/google_analytics_reports.rb', line 56

def viewport_report_request
  ReportRequest.new(
    view_id: VIEW_ID,
    date_ranges: [date_range],
    metrics: [metric_user],
    dimensions: [dimension_device_category, dimension_screen_resolution],
    order_bys: [{ field_name: 'ga:users', sort_order: 'DESCENDING' }],
    page_size: 100
  )
end