yt_analytics 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,6 +49,16 @@ class YTAnalytics
49
49
  client.temporal_totals('month',self.user_id, options)
50
50
  end
51
51
 
52
+ ### DEMOGRAPHICS METRICS
53
+ def demographic_percentages(options = {})
54
+ client.demographic_percentages(self.user_id, options)
55
+ end
56
+
57
+ ### WATCH TIME METRICS
58
+ def day_watch_time(options = {})
59
+ client.watch_time_metrics('day', self.user_id, options)
60
+ end
61
+
52
62
  private
53
63
 
54
64
  def client
@@ -0,0 +1,64 @@
1
+ class YTAnalytics
2
+ module Model #:nodoc:
3
+ class DemographicMetrics #:nodoc:
4
+
5
+ include YTAnalytics::Logging
6
+
7
+ attr_accessor :end_date, :age13_17female, :age55_64female, :age65_female, :age25_34male, :age25_34female, :age55_64male,
8
+ :age45_54female, :age35_44female, :age18_24female, :age65_male, :age35_44male, :age45_54male, :age18_24male, :age13_17male
9
+
10
+ def initialize params
11
+ @age13_17female = params[:age13_17female]
12
+ @age55_64female = params[:age55_64female]
13
+ @age65_female = params[:age65_female]
14
+ @age25_34male = params[:age25_34male]
15
+ @age25_34female = params[:age25_34female]
16
+ @age55_64male = params[:age55_64male]
17
+ @age45_54female = params[:age45_54female]
18
+ @age35_44female = params[:age35_44female]
19
+ @age18_24female = params[:age18_24female]
20
+ @age65_male = params[:age65_male]
21
+ @age35_44male = params[:age35_44male]
22
+ @age45_54male = params[:age45_54male]
23
+ @age18_24male = params[:age18_24male]
24
+ @age13_17male = params[:age13_17male]
25
+ end
26
+
27
+ def self.female
28
+ return age13_17female + age18_24female + age25_34female + age35_44female + age45_54female + age55_64female + age65_female
29
+ end
30
+
31
+ def self.male
32
+ return age13_17male + age18_24male + age25_34male + age35_44male + age45_54male + age55_64male + age65_male
33
+ end
34
+
35
+ def self.age13_17
36
+ age13_17male + age13_17female
37
+ end
38
+
39
+ def self.age18_24
40
+ age18_24male + age18_24female
41
+ end
42
+
43
+ def self.age25_34
44
+ age25_34male + age25_34female
45
+ end
46
+
47
+ def self.age35_44
48
+ age35_44male + age35_44female
49
+ end
50
+
51
+ def self.age45_54
52
+ age45_54male + age45_54female
53
+ end
54
+
55
+ def self.age55_64
56
+ age55_64male + age55_64female
57
+ end
58
+
59
+ def self.age65
60
+ age65_male + age65_female
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,17 @@
1
+ class YTAnalytics
2
+ module Model #:nodoc:
3
+ class WatchTimeMetrics #:nodoc:
4
+
5
+ include YTAnalytics::Logging
6
+ attr_accessor :end_date, :estimatedMinutesWatched, :averageViewDuration, :averageViewPercentage
7
+
8
+ def initialize params
9
+ @end_date = params[:endDate] if params[:endDate]
10
+ @estimatedMinutesWatched = params[:estimatedMinutesWatched] if params[:estimatedMinutesWatched]
11
+ @averageViewDuration = params[:averageViewDuration] if params[:averageViewDuration]
12
+ @averageViewPercentage = params[:averageViewPercentage] if params[:averageViewPercentage]
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -51,7 +51,7 @@ class YTAnalytics
51
51
 
52
52
  headers.each_with_index do |column,i|
53
53
  if column["columnType"] == "DIMENSION"
54
- metrics[:endDate] = Date.strptime row[i], "%Y-%m-%d"
54
+ metrics[:endDate] = Date.strptime row[i], row[i].length == 7 ? "%Y-%m" : "%Y-%m-%d"
55
55
  elsif column["columnType"] == "METRIC"
56
56
  metrics[eval(":" + column["name"])] = row[i]
57
57
  end
@@ -65,6 +65,52 @@ class YTAnalytics
65
65
  end
66
66
  end
67
67
 
68
+ class DemographicParser < FeedParser
69
+
70
+ private
71
+ def parse_content(content)
72
+ temporal_metrics = []
73
+ if content.is_a? Hash and content["rows"].is_a? Array and content["rows"].length > 0
74
+ metrics = {}
75
+
76
+ content["rows"].each do |row|
77
+ metrics[eval(":" + row[0].to_s.underscore + row[1].to_s.underscore)] = row[2]
78
+ end
79
+ end
80
+ YouTubeIt::Request::DemographicMetrics.new(metrics)
81
+ end
82
+ end
83
+
84
+
85
+ class WatchTimeParser < FeedParser
86
+
87
+ private
88
+ def parse_content(content)
89
+ watch_time_metrics = []
90
+ if content.is_a? Hash and content["rows"].is_a? Array and content["rows"].length > 0
91
+
92
+ headers = content["columnHeaders"]
93
+
94
+ content["rows"].each do |row|
95
+ metrics = {}
96
+
97
+ headers.each_with_index do |column,i|
98
+ if column["columnType"] == "DIMENSION"
99
+ metrics[:endDate] = Date.strptime row[i], "%Y-%m-%d"
100
+ elsif column["columnType"] == "METRIC"
101
+ metrics[eval(":" + column["name"])] = row[i]
102
+ end
103
+ end
104
+ watch_time_metrics.push(YTAnalytics::Model::WatchTimeMetrics.new(metrics))
105
+ end
106
+
107
+ watch_time_metrics.sort { |a,b| a.end_date <=> b.end_date }
108
+ end
109
+ watch_time_metrics
110
+ end
111
+ end
112
+
113
+
68
114
  class AnalyticsParser < FeedParser #:nodoc:
69
115
 
70
116
  private
@@ -56,10 +56,20 @@ class YTAnalytics
56
56
 
57
57
  def temporal_totals(dimension, user_id, options)
58
58
  #dimension is either day, 7DayTotals, 30DayTotals, or month
59
-
60
59
  opts = {'ids' => "channel==#{user_id}", 'dimensions' => dimension}
61
- opts['start-date'] = (options['start-date'].strftime("%Y-%m-%d") if options['start-date']) || 1.day.ago.strftime("%Y-%m-%d")
62
- opts['end-date'] = (options['end-date'].strftime("%Y-%m-%d") if options['end-date']) || 1.day.ago.strftime("%Y-%m-%d")
60
+
61
+ start_date = options['start-date'] || 2.days.ago
62
+ end_date = options['end-date'] || 2.days.ago
63
+
64
+ if (dimension == "month")
65
+ start_date = start_date.at_beginning_of_month
66
+ end_date = end_date.at_beginning_of_month
67
+ end
68
+
69
+ opts['start-date'] = start_date.strftime("%Y-%m-%d")
70
+ opts['end-date'] = end_date.strftime("%Y-%m-%d")
71
+ opts['filters'] = options['filters']
72
+
63
73
  if options['metrics'].class == Array
64
74
  opts['metrics'] = options['metrics'].join(",")
65
75
  elsif options['metrics'].class == String
@@ -75,6 +85,44 @@ class YTAnalytics
75
85
  return YTAnalytics::Parser::TemporalParser.new(content).parse
76
86
  end
77
87
 
88
+ def demographic_percentages(user_id, options)
89
+ opts = {'ids' => "channel==#{user_id}", 'dimensions' => 'ageGroup,gender'}
90
+ opts['start-date'] = (options['start-date'].strftime("%Y-%m-%d") if options['start-date']) || 2.day.ago.strftime("%Y-%m-%d")
91
+ opts['end-date'] = (options['end-date'].strftime("%Y-%m-%d") if options['end-date']) || 2.day.ago.strftime("%Y-%m-%d")
92
+ if options['metrics'].class == Array
93
+ opts['metrics'] = options['metrics'].join(",")
94
+ elsif options['metrics'].class == String
95
+ opts['metrics'] = options['metrics'].delete(" ")
96
+ else
97
+ opts['metrics'] = 'viewerPercentage'
98
+ end
99
+
100
+ get_url = "/youtube/analytics/v1/reports?"
101
+ get_url << opts.collect { |k,p| [k,p].join '=' }.join('&')
102
+ response = yt_session('https://www.googleapis.com').get(get_url)
103
+ content = JSON.parse(response.body)
104
+ return YTAnalytics::Parser::DemographicParser.new(content).parse
105
+ end
106
+
107
+ def watch_time_metrics(dimension, user_id, options)
108
+ #dimension is day
109
+ opts = {'ids' => "channel==#{user_id}", 'dimensions' => dimension}
110
+ opts['start-date'] = (options['start-date'].strftime("%Y-%m-%d") if options['start-date']) || 1.day.ago.strftime("%Y-%m-%d")
111
+ opts['end-date'] = (options['end-date'].strftime("%Y-%m-%d") if options['end-date']) || 1.day.ago.strftime("%Y-%m-%d")
112
+ if options['metrics'].class == Array
113
+ opts['metrics'] = options['metrics'].join(",")
114
+ elsif options['metrics'].class == String
115
+ opts['metrics'] = options['metrics'].delete(" ")
116
+ else
117
+ opts['metrics'] = 'estimatedMinutesWatched,averageViewDuration,averageViewPercentage'
118
+ end
119
+
120
+ get_url = "/youtube/analytics/v1/reports?"
121
+ get_url << opts.collect { |k,p| [k,p].join '=' }.join('&')
122
+ response = yt_session('https://www.googleapis.com').get(get_url)
123
+ content = JSON.parse(response.body)
124
+ return YTAnalytics::Parser::WatchTimeParser.new(content).parse
125
+ end
78
126
 
79
127
  private
80
128
 
@@ -1,4 +1,4 @@
1
1
  class YTAnalytics
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
4
4
 
data/lib/yt_analytics.rb CHANGED
@@ -63,6 +63,8 @@ end
63
63
  client
64
64
  parser
65
65
  model/temporal_metrics
66
+ model/demographic_metrics
67
+ model/watch_time_metrics
66
68
  request/authentication
67
69
  middleware/faraday_authheader.rb
68
70
  middleware/faraday_oauth.rb
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yt_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-11 00:00:00.000000000 Z
13
+ date: 2012-12-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri
@@ -121,7 +121,9 @@ files:
121
121
  - lib/yt_analytics/middleware/faraday_oauth.rb
122
122
  - lib/yt_analytics/middleware/faraday_oauth2.rb
123
123
  - lib/yt_analytics/middleware/faraday_yt_analytics.rb
124
+ - lib/yt_analytics/model/demographic_metrics.rb
124
125
  - lib/yt_analytics/model/temporal_metrics.rb
126
+ - lib/yt_analytics/model/watch_time_metrics.rb
125
127
  - lib/yt_analytics/parser.rb
126
128
  - lib/yt_analytics/request/authentication.rb
127
129
  - lib/yt_analytics/version.rb