Created
August 6, 2012 10:07
-
-
Save ricburton/3273139 to your computer and use it in GitHub Desktop.
dashboard query in need of cosmetic surgery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AnalyticsController < ApplicationController | |
def download | |
consumer = OAuth::Consumer.new('******.apps.googleusercontent.com', '******', | |
{:site => 'https://www.google.com', | |
:request_token_path => '/accounts/OAuthGetRequestToken', | |
:access_token_path => '/accounts/OAuthGetAccessToken', | |
:authorize_path => '/accounts/OAuthAuthorizeToken'} | |
) | |
token = '*****' | |
secret = '******' | |
access_token = OAuth::AccessToken.new(consumer, token, secret) | |
Garb::Session.access_token = access_token | |
profile = Garb::Management::Profile.all.detect {|p| p.web_property_id == 'UA-8697810-2'} | |
puts profile | |
today = Date.today | |
history = 30 # add 1 to this for the number of days that will be received | |
while history >= 0 | |
start_date = today - history | |
end_date = today - history | |
data = Hash.new | |
data["branded"] = profile.stats(start_date: start_date, end_date: end_date, filters: { :medium.matches => "organic", :keyword.contains => "wood"} ).first | |
data["nonbranded"] = profile.stats(start_date: start_date, end_date: end_date, filters: { :medium.matches => "organic", :keyword.does_not_contain => "wood|(not provided)|np|(not set)"} ).first | |
data["unknown"] = profile.stats(start_date: start_date, end_date: end_date, filters: { :medium.matches => "organic", :keyword.contains => "(not provided)|np|(not set)"} ).first | |
data["affiliate"] = profile.stats(start_date: start_date, end_date: end_date, filters: { :source.contains => "track.webgains.com"} ).first | |
data["social"] = profile.stats(start_date: start_date, end_date: end_date, filters: { :source.contains => "facebook\.com|twitter\.com|pinterest\.com|youtube|flickr" } ).first | |
data["referral"] = profile.stats(start_date: start_date, end_date: end_date, filters: { :medium.matches => "referral", :source.does_not_contain => "facebook\.com|twitter\.com|pinterest\.com|youtube|flickr|track.webgains.com" } ).first | |
data["paid"] = profile.stats(start_date: start_date, end_date: end_date, segment_id: '-4').first | |
data["direct"] = profile.stats(start_date: start_date, end_date: end_date, segment_id: '-7').first | |
data["email"] = profile.stats(start_date: start_date, end_date: end_date, filters: { :medium.contains => "email|Email"} ).first | |
data.each do |source,results| | |
data_check = Analytic.find_or_create_by_start_date_and_source(start_date, source) | |
data_check.update_attributes!({ | |
start_date: start_date, | |
end_date: end_date, | |
source: source, | |
site: "woodhouse", | |
visits: results.visits, | |
revenue: results.transaction_revenue, | |
revenue_per_transaction: results.revenue_per_transaction, | |
transactions: results.transactions, | |
items_per_purchase: results.items_per_purchase, | |
goal_starts: results.goal2_starts, | |
goal_completes: results.goal2_completions, | |
goal_conversion: results.goal2_conversion_rate, | |
goal_abandon: results.goal2_abandon_rate}) | |
puts source.to_s + " " + results.to_s | |
end | |
history -= 1 | |
puts "DONE A DAY!" | |
end | |
respond_to do |format| | |
format.html | |
end | |
end | |
end |
bostwick
commented
Aug 10, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment