Class: GRuby::Analytics
- Inherits:
-
Object
- Object
- GRuby::Analytics
- Defined in:
- lib/g_ruby/analytics.rb
Constant Summary collapse
- BASE_URL =
"https://www.googleapis.com/analytics/v3"
Class Method Summary collapse
-
.accounts(access_token, pid) ⇒ Object
GRuby::Analytics.accounts(access_token, pid).
- .generate_delta_explaination(d_this, d_last, refresh) ⇒ Object
-
.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).
-
.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).
-
.goals(access_token, aid, wid, pid, start_date, end_date) ⇒ Object
GRuby::Analytics.goals(ak.app_password, ak.ga_account_id, ak.ga_webproperty_id, ak.entity_name, start_date, end_date).
Class Method Details
.accounts(access_token, pid) ⇒ Object
GRuby::Analytics.accounts(access_token, pid)
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/g_ruby/analytics.rb', line 10 def self.accounts(access_token, pid) url = "#{BASE_URL}/management/accounts?access_token=#{access_token}" a = GRuby::Util.get_json(Nestful.get(url)) response_obj = [] if !a.blank? if !a["items"].blank? if !a["items"].first.blank? a["items"].each do |account| if !account["id"].blank? url_w = "#{BASE_URL}/management/accounts/#{account['id'].to_s}/webproperties?access_token=#{access_token}" w = GRuby::Util.get_json(Nestful.get(url_w)) if !w.blank? if !w["items"].blank? if !w["items"].first.blank? w["items"].each do |webprop| if !webprop["id"].blank? url_p = "#{BASE_URL}/management/accounts/#{account['id'].to_s}/webproperties/#{webprop['id'].to_s}/profiles?access_token=#{access_token}" p = GRuby::Util.get_json(Nestful.get(url_p)) if !p.blank? if !p["items"].blank? if !p["items"].first.blank? p["items"].each do |prof| if !prof["id"].blank? if pid == prof["id"] response_obj << [account["id"], webprop["id"], prof["created"]] end end end end end end end end end end end end end end end end return response_obj end |
.generate_delta_explaination(d_this, d_last, refresh) ⇒ Object
185 186 187 188 189 190 191 192 193 |
# File 'lib/g_ruby/analytics.rb', line 185 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)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/g_ruby/analytics.rb', line 76 def self.get(access_token, start_date, end_date, profile_id, metrics, dimensions=nil, sort=nil, limit=nil) #begin url = "#{BASE_URL}/data/ga?access_token=#{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)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/g_ruby/analytics.rb', line 102 def self.get_with_delta(access_token, start_date, end_date, profile_id, metrics, dimensions=nil, sort=nil, limit=nil, refresh=nil) #begin response_obj = [] 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) if !a0.blank? 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 a30[:status] == "failed" elsif a60.class.to_s != "Array" return a60 if a60[:status] == "failed" end 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) if d_last == 0 response_obj << {tag: metric.gsub("ga:", ""), val: a0[metric], delta: 0, delta_explaination: dd} else response_obj << {tag: metric.gsub("ga:", ""), val: a0[metric], delta: ((d_this - d_last) * 100 / d_last), delta_explaination: dd} end 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) if d_last == 0 del = 0 else del = ((d_this.to_i - d_last.to_i) * 100 / d_last.to_f) end 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) if d_last.to_f == 0 del = 0 else del = ((d_this.to_i - d_last.to_i) * 100 / d_last.to_f) end response_obj << {tag: i[0], val: i[2], dimension: k3, delta: del, delta_explaination: dd} end end end return response_obj #rescue => e #return {status: "failed", error: e.inspect} #end end |
.goals(access_token, aid, wid, pid, start_date, end_date) ⇒ Object
GRuby::Analytics.goals(ak.app_password, ak.ga_account_id, ak.ga_webproperty_id, ak.entity_name, start_date, end_date)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/g_ruby/analytics.rb', line 57 def self.goals(access_token, aid, wid, pid, start_date, end_date) url = "#{BASE_URL}/management/accounts/#{aid}/webproperties/#{wid}/profiles/#{pid}/goals?access_token=#{access_token}" a = GRuby::Util.get_json(Nestful.get(url)) response_obj = [] if !a["items"].blank? if !a["items"].first.blank? a["items"].each do |b| metrics = "ga:goal#{b['id']}Completions,ga:goal#{b['id']}ConversionRate,ga:goal#{b['id']}AbandonRate" goals_response = GRuby::Analytics.get(access_token, start_date, end_date, pid, metrics) response_obj << [b["id"], b["name"], b["value"], b["active"], b["created"], b["updated"], goals_response["ga:goal#{b['id']}Completions"], goals_response["ga:goal#{b['id']}ConversionRate"], goals_response["ga:goal#{b['id']}AbandonRate"]] end end end return response_obj end |