Class: Omnom::Source::GoogleAnalytics::Default

Inherits:
Base
  • Object
show all
Defined in:
lib/omnom/source/google_analytics/default.rb

Direct Known Subclasses

Pageviews, Visitors

Instance Attribute Summary

Attributes inherited from Base

#config, #feed_key, #key, #options, #settings, #source_id

Instance Method Summary collapse

Methods inherited from Base

config, configure, cron, every, feed_url, full_key, guid_namespace, icon, icon_from_url, inherited, #initialize, key, required_config, required_options, #update, url

Methods included from ParserMethods

#html_to_text

Constructor Details

This class inherits a constructor from Omnom::Source::Base

Instance Method Details

#after_initialize ⇒ Object



10
11
12
13
# File 'lib/omnom/source/google_analytics/default.rb', line 10

def after_initialize
  set_options
  initialize_legato
end

#attribute_value(attribute, response) ⇒ Object



66
67
68
69
# File 'lib/omnom/source/google_analytics/default.rb', line 66

def attribute_value(attribute, response)
  return @options[:attribute_methods][attribute].call(response) if @options[:attribute_methods][attribute].present?
  send(attribute, response)
end

#author_name(response) ⇒ Object



89
90
91
# File 'lib/omnom/source/google_analytics/default.rb', line 89

def author_name(response)
  @legato_profile.name
end

#author_url(response) ⇒ Object



93
94
95
# File 'lib/omnom/source/google_analytics/default.rb', line 93

def author_url(response)
  "http://#{@legato_profile.name}"
end

#get_raw_posts ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/omnom/source/google_analytics/default.rb', line 39

def get_raw_posts
  @legato_filters = @options[:filters]
  @legato_filters.each do |key, value|
    @legato_filters[key] = value.is_a?(Proc) ? value.call : value
  end
  query = @legato_model.results(@legato_profile, @legato_filters)
  query.collect do |result|
    {
      query: query,
      profile: query.profile,
      result: result
    }
  end
end

#guid(response) ⇒ Object



76
77
78
# File 'lib/omnom/source/google_analytics/default.rb', line 76

def guid(response)
  "#{@legato_profile.name}:#{metric}:#{published_at(response)}"
end

#initialize_legato ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/omnom/source/google_analytics/default.rb', line 28

def initialize_legato
  @legato_model_name = "LegatoModel#{@source_id}"
  @legato_model = create_legato_model
  @legato_user = ::Legato::User.new(get_access_token)
  raise "Unable to create Legato user in #{self.class}" and return if @legato_user.blank?
  @legato_profile = @legato_user.profiles.select do |profile|
    profile.attributes['internalWebPropertyId'] == @options[:profile_id].to_s
  end.first
  raise "Profile with ID '#{@options[:profile_id]}' not found in #{self.class}" and return if @legato_profile.blank?
end

#metric ⇒ Object



101
102
103
# File 'lib/omnom/source/google_analytics/default.rb', line 101

def metric
  @options[:metrics].first
end

#other(response) ⇒ Object



97
98
99
# File 'lib/omnom/source/google_analytics/default.rb', line 97

def other(response)
  response[:result].marshal_dump
end

#post_attributes(response) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/omnom/source/google_analytics/default.rb', line 54

def post_attributes(response)
  {
    title: attribute_value(:title, response),
    guid: attribute_value(:guid, response),
    url: attribute_value(:url, response),
    published_at: attribute_value(:published_at, response),
    author_name: attribute_value(:author_name, response),
    author_url: attribute_value(:author_url, response),
    other: attribute_value(:other, response)
  }
end

#published_at(response) ⇒ Object



84
85
86
87
# File 'lib/omnom/source/google_analytics/default.rb', line 84

def published_at(response)
  return nil if @legato_filters[:end_date].blank?
  Time.parse(@legato_filters[:end_date].to_s)
end

#set_options ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/omnom/source/google_analytics/default.rb', line 15

def set_options
  defaults = {
    metrics: nil,
    dimensions: nil,
    filters: {
      start_date: Proc.new { Date.yesterday },
      end_date: Proc.new { Date.yesterday }
    },
    attribute_methods: {}
  }
  @options.reverse_merge!(defaults)
end

#title(response) ⇒ Object



71
72
73
74
# File 'lib/omnom/source/google_analytics/default.rb', line 71

def title(response)
  date = @legato_filters[:end_date].strftime('%a, %_m/%e').squeeze
  "#{response[:result].send(metric)} #{metric} on #{@legato_profile.name} (#{date})"
end

#url(response) ⇒ Object



80
81
82
# File 'lib/omnom/source/google_analytics/default.rb', line 80

def url(response)
  'https://www.google.com/analytics/'
end