Rugalytics | Ruby Manor Nov 2008

9,864 views

Published on

Rugalytics - making a Ruby API to Google Analytics data - presentation slides from Ruby Manor 2008

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
9,864
On SlideShare
0
From Embeds
0
Number of Embeds
92
Actions
Shares
0
Downloads
42
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Rugalytics | Ruby Manor Nov 2008

  1. Rugalytics Photo: Archie McPhee Seattle http://flickr.com/photos/archiemcphee/1444560125/
  2. got
  3. website
  4. also
  5. Ruby API?
  6. jnunemaker
  7. Statwhore Photo: CARLOS62 http://flickr.com/photos/82887550@N00/352151895/
  8. logged in
  9. Analytics account
  10. grabbed
  11. <XML> reports
  12. <MiniTable id=quot;BrowserMiniTablequot;> <Row> <Cell> <Content> <Value>8,995</Value> </Content> </Cell> <Cell> <Content> <Value>70.79%</Value> </Content> </Cell> <PrimaryKey>IE</PrimaryKey> </Row> <Row>
  13. hardcoded
  14. accessors
  15. def pageviews end def visits end
  16. cool
  17. wanted
  18. MORE
  19. wanted
  20. to emerge class definitions from data at runtime Photo: brilarian http://flickr.com/photos/bybri/4443691/
  21. forked
  22. Statwhore
  23. kept
  24. login logic
  25. Google::Base
  26. grabbed
  27. C,S,V reports
  28. #----------------------- # BrowserMiniTable #----------------------- Browser,Visits,% visits IE,89950,0.70787754 Firefox,27980,0.22019359 Safari,5640,0.044384984
  29. made methods
  30. from data
  31. Morph Photo: Salt Fired http://www.flickr.com/photos/saltfired/201998836/
  32. module Rugalytics class Item include Morph
  33. def initialize labels, values
  34. labels.each_with_index do |label, index|
  35. attribute = normalize(label) value = values[index] morph(attribute, value)
  36. under the rug Photo: LizMarie http://flickr.com/photos/perspicacious/104917207/
  37. def morph_method_missing symbol,*args attribute = symbol.to_s.chomp '=' base.class_eval quot;def #{attribute}; @#{attribute}; end; def #{attribute}=(value); @#{attribute} = value; endquot; send(symbol, *args)
  38. > require 'ruby2ruby'
  39. > klass = Rugalytics::Item > y Ruby2Ruby.translate(klass)
  40. class Rugalytics::Item < Object def bounce_rate @bounce_rate end def bounce_rate=(value) @bounce_rate = value end def dollar_index @dollar_index end def dollar_index=(value) @dollar_index = value end def initialize(labels, values, base_url) labels.each_with_index do |label, index| ...
  41. controller
  42. def top_content days profile = Rugalytics.default_profile from_date = Date.today - days report = profile.top_content_report :from => from_date
  43. items = report.items items = items.sort_by { |i| i.unique_pageviews.to_i} items = items.reverse items = items[0..9] if items.size >10 items end
  44. view
  45. %ul - @top_pages.each do |item| %li = link_to(item.page_title, item.url) %span.page_views =quot;(#{item.unique_pageviews})quot;
  46. lots of reports Photo: CloCkWeRX http://flickr.com/photos/clockwerx/9267076/
  47. def get_report_csv(options={}) options = set_default_options(options) params = convert_options_to_uri_params(options) self.class.get( quot;https://google.com/analytics/ reporting/exportquot;, :query_hash => params) end
  48. def set_default_options(options) options.reverse_merge!({ :report => 'Dashboard', :from => a_month_ago, :to => today, :tab => 0, :format => FORMAT_CSV, :rows => 50, :compute => 'average', :gdfmt => 'nth_day', :view => 0 })
  49. def convert_options_to_uri_params(options) params = { :pdr => quot;#{options[:from]}- #{options[:to]}quot;, :rpt => quot;#{options[:report]}Reportquot;, :cmp => options[:compute], :fmt => options[:format], :view => options[:view], :tab => options[:tab], :trows=> options[:rows], :gdfmt=> options[:gdfmt], :id => profile_id }
  50. > profile.report_names => [quot;ad_versions_reportquot;, quot;adwords_reportquot;, quot;all_sources_reportquot;, quot;average_pageviews_reportquot;, quot;bounce_rate_reportquot;,quot;browsers_reportquot;,quot;campaigns_reportquot;, quot;colors_reportquot;, quot;content_by_title_reportquot;, quot;content_drilldown_reportquot;, quot;content_reportquot;, quot;dashboard_reportquot;, quot;depth_of_visit_reportquot;, quot;direct_sources_reportquot;, quot;entrances_reportquot;, quot;exits_reportquot;,quot;flash_reportquot;,quot;geo_map_reportquot;,quot;hostnames_ reportquot;,quot;java_reportquot;,quot;keyword_position_reportquot;, quot;keywords_reportquot;,quot;languages_reportquot;,quot;length_of_visit_repo rtquot;,quot;loyalty_reportquot;,quot;networks_reportquot;,quot;os_browsers_report quot;,quot;pageviews_reportquot;,quot;platforms_reportquot;,quot;recency_reportquot;,quot; referring_sources_reportquot;, quot;resolutions_reportquot;, quot;search_engines_reportquot;,quot;speeds_reportquot;,quot;time_on_site_repo rtquot;,quot;top_content_detail_keywords_reportquot;,quot;top_content_deta il_navigation_reportquot;, quot;top_content_detail_path_reportquot;, quot;top_content_detail_sources_reportquot;, quot;top_content_reportquot;, quot;traffic_sources_reportquot;, quot;unique_visitors_reportquot;, quot;visitor_types_reportquot;, quot;visitors_overview_reportquot;, quot;visits_reportquot;]
  51. it
  52. worked
  53. but Photo: moriza http://flickr.com/photos/moriza/72551421/
  54. English
  55. UK
  56. 28 August 2008
  57. US
  58. quot;August 28, 2008quot;
  59. UK
  60. Page Views
  61. item.page_views
  62. US
  63. Pageviews
  64. item.pageviews
  65. Italiano
  66. 28 agosto 2008
  67. Pagine visualizzate
  68. item. pagine_visualizzate
  69. Google: Settings -> Language:
  70. choose UK English
  71. OR
  72. choose US English
  73. Rug rat Photo: Ozyman http://flickr.com/photos/ozyman/465113688/
  74. local server
  75. http://localhost:8888/ top_content_detail_keywords ?url=/mps
  76. serves json
  77. def call(env) path = env['PATH_INFO'].tr('/','') request = Rack::Request.new(env) report_name = (path + '_report').to_sym params = request.GET.symbolize_keys report = @profile.send(report_name, params) data = { :report_name => report.name, :items => report.items} [200, {'Content-Type' => quot;application/jsonquot;}, data.to_json ] end
  78. { quot;report_namequot;: quot;Entrance Keywords:,/mpsquot;, quot;itemsquot;: [ {quot;bounce_ratequot;: quot;0.0quot;, quot;pageviewsquot;: quot;17quot;, quot;percentage_exitquot;: quot;0.058823529quot;, quot;time_on_pagequot;: quot;21.9375quot;, quot;dollar_indexquot;: quot;0.0quot;, quot;keywordquot;: quot;gordon copelandquot;, quot;unique_pageviewsquot;: quot;10quot;},
  79. greasemonkey script
  80. http://localhost:8888/ top_content_detail_keywords ?url=/mps
  81. http://localhost:8888/ keyword_detail? keywords=party list mps nz &segment=city
  82. http://localhost:8888/ keyword_detail? keywords=party list mps nz &segment=organization
  83. Production?
  84. Future? have fun! wrap an OS analytics lib?
  85. a lytics github.com/robmckinnon rugalytics morph

×