Class: GRuby::Analytics

Inherits:
Object
  • Object
show all
Defined in:
lib/g_ruby/analytics.rb

Constant Summary collapse

GA_BASE =
"https://www.googleapis.com/analytics/v3/data/ga?access_token="

Class Method Summary collapse

Class Method Details

.generate_delta_explaination(d_this, d_last, refresh) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/g_ruby/analytics.rb', line 104

def self.generate_delta_explaination(d_this, d_last, refresh)
  if !d_this.blank? and !d_last.blank?
    d_this_to_s = d_this.to_s.gsub(".0", "")
    d_last_to_s = d_last.to_s.gsub(".0", "")
    return (refresh == "last_30") ? "Last 31 to 60 days: #{d_last_to_s}" : "Last 30 days: #{d_this_to_s}; Last 31 to 60 days: #{d_last_to_s}"
  else
    return nil
  end
end

.get(access_token, start_date, end_date, profile_id, metrics, dimensions = nil, sort = nil, limit = nil) ⇒ Object

GRuby::Analytics.get(access_token, start_date, end_date, profile_id, metrics, dimensions, sort, max_results)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/g_ruby/analytics.rb', line 10

def self.get(access_token, start_date, end_date, profile_id, metrics, dimensions=nil, sort=nil, limit=nil)
  #begin
    url = GA_BASE + "#{access_token}&start-date=#{start_date}&end-date=#{end_date}&ids=ga:#{profile_id}&metrics=#{metrics}"
    url = url + "&dimensions=#{dimensions}"       if !dimensions.blank?
    url = url + "&sort=#{sort}"                   if !sort.blank?
    url = url + "&max-results=#{limit}"           if !limit.blank?
    a = GRuby::Util.get_json(Nestful.get(url))
    if !a.blank?
      if !a["totalsForAllResults"].blank?
        if dimensions.blank?
          return a["totalsForAllResults"]
        else
          return a["rows"]
        end
      end
    end
    return nil
  #rescue => e
    #return {status: "failed", error: e.inspect}
  #end
end

.get_with_delta(access_token, start_date, end_date, profile_id, metrics, dimensions = nil, sort = nil, limit = nil, refresh = nil) ⇒ Object

GRuby::Analytics.get_with_delta(ak.app_password, genesis, date_today, ak.entity_name, “ga:visits”, “ga:socialNetwork”, “-ga:visits”, 11, refresh)

GRuby::Analytics.get_with_delta(access_token, start_date, end_date, profile_id, metrics, dimensions, sort, max_results)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/g_ruby/analytics.rb', line 36

def self.get_with_delta(access_token, start_date, end_date, profile_id, metrics, dimensions=nil, sort=nil, limit=nil, refresh=nil)
  #begin
    d30_days_ago = (Date.today - 30).to_time.strftime("%Y-%m-%d").to_s
    d60_days_ago = (Date.today - 60).to_time.strftime("%Y-%m-%d").to_s
    date_today   = Time.now.strftime("%Y-%m-%d")

    a0 = GRuby::Analytics.get(access_token, start_date, end_date, profile_id, metrics, dimensions, sort, limit)
    a30 = GRuby::Analytics.get(access_token, d30_days_ago, date_today, profile_id, metrics, dimensions, sort, nil)
    a60 = GRuby::Analytics.get(access_token, d60_days_ago, d30_days_ago, profile_id, metrics, dimensions, sort, nil)
    
    if a0.class.to_s != "Array"
      return a0 if a0[:status] == "failed"
    elsif a30.class.to_s != "Array"
      return a30 if a0[:status] == "failed"
    elsif a60.class.to_s != "Array"
      return a60 if a0[:status] == "failed"
    end
    
    response_obj = []

    if dimensions.blank?
      metrics.split(",").each do |metric|
        d_this = a30[metric].to_i
        d_last = a60[metric].to_i
        dd = GRuby::Analytics.generate_delta_explaination(d_this, d_last, refresh)
        response_obj << {tag: metric.gsub("ga:", ""), val: a0[metric], delta: ((d_this - d_last) * 100 / d_last), delta_explaination: dd}
      end
    elsif dimensions.split(",").count == 1 and metrics.split(",").count == 1
      a0.each do |i|
        d_this = nil
        d_last = nil
        a30.each do |j|
          d_this = j[1].to_i if j[0] == i[0]
        end
        a60.each do |k|
          d_last = k[1].to_i if k[0] == i[0]
        end
        dd = GRuby::Analytics.generate_delta_explaination(d_this, d_last, refresh)
        del = ((d_this.to_i - d_last.to_i) * 100 / d_last.to_f)
        response_obj << {tag: metrics.gsub("ga:", ""), val: i[1], dimension: i[0], delta: del, delta_explaination: dd}
      end
    elsif dimensions.split(",").count > 1 and metrics.split(",").count == 1
      a0.each do |i|
        d_this = nil
        d_last = nil
        if dimensions == "ga:source,ga:medium"
          k3 = i[1]=='referral' ? 'OtherSources' : (i[1]=='(none)' and i[0]=="(direct)") ? 'DirectSource' : 'searchEngine'
        elsif dimensions == "ga:keyword,ga:medium"
          k3 = i[1] == 'organic' ? 'OrganicKeywords' : 'PaidKeywords'
        end
        a30.each do |j|
          d_this = j[2] if (j[0] == i[0] and j[1] == i[1])
        end
        a60.each do |k|
          d_last = k[2] if (k[0] == i[0] and k[1] == i[1])
        end
        dd = GRuby::Analytics.generate_delta_explaination(d_this, d_last, refresh)
        del = ((d_this.to_i - d_last.to_i) * 100 / d_last.to_f)
        response_obj << {tag: i[0], val: i[2], dimension: k3, delta: del, delta_explaination: dd}
      end
    end

    return response_obj  
  #rescue => e
    #return {status: "failed", error: e.inspect}
  #end
end