Class: Adminpanel::AnalyticsController

Inherits:
ApplicationController show all
Includes:
Adminpanel::Analytics::InstagramAnalytics, Adminpanel::Analytics::TwitterAnalytics
Defined in:
app/controllers/adminpanel/analytics_controller.rb

Constant Summary collapse

API_VERSION =
'v3'
CACHED_API_FILE =
"#{Rails.root}/tmp/cache/analytics-#{API_VERSION}.cache"

Instance Method Summary collapse

Methods included from Adminpanel::Analytics::InstagramAnalytics

#instagram_comment

Methods included from Adminpanel::Analytics::TwitterAnalytics

#favorite_tweet, #reply_to_tweet, #retweet_tweet

Methods included from TwitterActions

#twitter_publish

Methods included from FacebookActions

#fb_choose_page, #fb_publish, #fb_save_token

Methods included from GalleryzableActions

#move_gallery_better, #move_gallery_worst

Methods included from SortableActions

#move_to_better, #move_to_worst

Methods included from RestActions

#create, #destroy, #edit, #new, #show, #update

Methods included from SessionsHelper

#current_user, #current_user=, #sign_in, #sign_out, #signed_in?

Instance Method Details

#fbObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
# File 'app/controllers/adminpanel/analytics_controller.rb', line 84

def fb
  authorize! :read, Adminpanel::Analytic
  if params[:insight].present?
    period = params[:insight]
  else
    period = 'day' #default period
  end
  page_graph = Koala::Facebook::API.new(@auth.value)
  @impressions,
  @impressions_unique,

  @new_likes,
  @total_likes,

  @hidden,
  @hidden_unique,

  @consumptions,
  @consumptions_unique,

  @views,
  @views_unique,

  @stories = page_graph.batch do |api|
    #all information on same request
    api.get_connections('me', 'insights', metric: 'page_impressions', period: period) #eye
    api.get_connections('me', 'insights', metric: 'page_impressions_unique', period: period) #eye

    api.get_connections('me', 'insights', metric: 'page_fan_adds') #fb-thumb
    api.get_connections('me', 'insights', metric: 'page_fans') #fb-thumb

    api.get_connections('me', 'insights', metric: 'page_negative_feedback', period: period)
    api.get_connections('me', 'insights', metric: 'page_negative_feedback_unique', period: period)

    api.get_connections('me', 'insights', metric: 'page_consumptions', period: period)
    api.get_connections('me', 'insights', metric: 'page_consumptions_unique', period: period)

    api.get_connections('me', 'insights', metric: 'page_views')
    api.get_connections('me', 'insights', metric: 'page_views_unique')

    api.get_connections('me', 'insights', metric: 'page_stories', period: period)
  end
end

#googleObject



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
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
# File 'app/controllers/adminpanel/analytics_controller.rb', line 15

def google
  authorize! :read, Adminpanel::Analytic

  unless Adminpanel.analytics_profile_id.nil? || Adminpanel.analytics_key_filename.nil? || Adminpanel..nil?
     = Adminpanel. # Email of service account
    key_file = "#{Rails.root}/#{Adminpanel.analytics_key_path}/#{Adminpanel.analytics_key_filename}" # File containing your private key
    key_secret = 'notasecret' # Password to unlock private key
    profileID = Adminpanel.analytics_profile_id # Analytics profile ID.


    client = Google::APIClient.new(
      :application_name => Adminpanel.analytics_application_name,
      :application_version => Adminpanel.analytics_application_version
    )

    analytics = nil
    # Load cached discovered API, if it exists. This prevents retrieving the
    # discovery document on every run, saving a round-trip to the discovery service.
    if File.exists? CACHED_API_FILE
      File.open(CACHED_API_FILE) do |file|
        analytics = Marshal.load(file)
      end
    else
      analytics = client.discovered_api('analytics', API_VERSION)
      File.open(CACHED_API_FILE, 'w') do |file|
        Marshal.dump(analytics, file)
      end
    end

    key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
    client.authorization = Signet::OAuth2::Client.new(
      :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
      :audience => 'https://accounts.google.com/o/oauth2/token',
      :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
      :scope => 'https://www.googleapis.com/auth/analytics.readonly',
      :issuer => ,
      :signing_key => key
    )
    # Request a token for our service account
    client.authorization.fetch_access_token!

    startDate = DateTime.now.prev_day(15).strftime("%Y-%m-%d")
    endDate = DateTime.now.strftime("%Y-%m-%d")

    visitCount = client.execute(
      :api_method => analytics.data.ga.get,
      :parameters => {
        'ids' => "ga:#{profileID}",
        'start-date' => startDate,
        'end-date' => endDate,
        'dimensions' => "ga:day,ga:month, ga:year",
        'metrics' => "ga:visits",
        'sort' => "ga:year,ga:month,ga:day"
      }
    )

    @visits = visitCount.data.rows.collect do |r|
      {
        date: "#{r[2]}/#{r[1]}/#{r[0]}",
        visits: r[3]
      }
    end
  end
  respond_to do |format|
    format.html
    format.json {render :json => {:visit_count => @visitCount, :visits => @visits, :visit_dates => @visitDates }}
  end
end

#indexObject



12
13
# File 'app/controllers/adminpanel/analytics_controller.rb', line 12

def index
end

#instagramObject



149
150
151
152
153
154
# File 'app/controllers/adminpanel/analytics_controller.rb', line 149

def instagram
  authorize! :read, Adminpanel::Analytic
  if !@instagram_token.nil?
    @user = @instagram_client.user
  end
end

#twitterObject

uses @client to fetch replies and tweets, for some statics



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/adminpanel/analytics_controller.rb', line 129

def twitter
  authorize! :read, Adminpanel::Analytic
  if !@twitter_token.nil? && !@twitter_secret.nil?
    @favorites = 0.0
    @retweets = 0.0
    @twitter_user = @twitter_client.user

    # 20 is the number that we're using to measure statics.
    @twitter_client.user_timeline(@twitter_user.username).take(20).collect do |tweet|
      @favorites = @favorites + tweet.favorite_count.to_f
      @retweets = @retweets + tweet.retweet_count.to_f
    end

    @tweets = @twitter_client.mentions_timeline.take(5)

    @favorites = @favorites / 20.0
    @retweets = @retweets / 20.0
  end
end