Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update the documentation

  • Loading branch information...
commit a3923a85d6ff193017a4aabba12fcb1a7fba049f 1 parent 526d818
Benoit Garret authored
1  .gitignore
@@ -2,3 +2,4 @@
2 2 .bundle
3 3 Gemfile.lock
4 4 pkg/*
  5 +.yardoc
1  .yardopts
... ... @@ -0,0 +1 @@
  1 +-m markdown --no-private --plugin rails
5 README.markdown
Source Rendered
... ... @@ -1,7 +1,8 @@
1   -In config/environments/production.rb :
  1 +In `config/environments/production.rb` :
2 2
  3 + # replace this with your tracker code
3 4 GAR.tracker = "UA-xxxxxx-x"
4 5
5   -In your layout, between the `<head>` tag :
  6 +In your layout, in the `<head>` tag :
6 7
7 8 <%= analytics_init %>
5 google-analytics-rails.gemspec
@@ -4,7 +4,7 @@ require "google-analytics-rails/version"
4 4
5 5 Gem::Specification.new do |s|
6 6 s.name = "google-analytics-rails"
7   - s.version = Google::Analytics::Rails::VERSION
  7 + s.version = GoogleAnalytics::Rails::VERSION
8 8 s.platform = Gem::Platform::RUBY
9 9 s.authors = ["Benoit Garret"]
10 10 s.email = ["benoit.garret@gadz.org"]
@@ -15,6 +15,9 @@ Gem::Specification.new do |s|
15 15 s.rubyforge_project = "google-analytics-rails"
16 16
17 17 s.add_dependency "google_analytics_tools"
  18 + s.add_development_dependency "yard"
  19 + s.add_development_dependency "yard-rails"
  20 + s.add_development_dependency "redcarpet"
18 21
19 22 s.files = `git ls-files`.split("\n")
20 23 s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
5 lib/google-analytics-rails.rb
... ... @@ -1,16 +1,21 @@
1 1 module GoogleAnalytics
2 2 module Rails
  3 + # @private
3 4 PLACEHOLDER_TRACKER = "UA-xxxxxx-x"
4 5
  6 + # The current tracker id (*UA-xxxxxx-x*).
5 7 mattr_accessor :tracker
  8 + # @private
6 9 @@tracker = PLACEHOLDER_TRACKER
7 10
  11 + # @return [Boolean]
8 12 def self.valid_tracker?
9 13 tracker.blank? || tracker == PLACEHOLDER_TRACKER ? false : true
10 14 end
11 15 end
12 16 end
13 17
  18 +# Alias for GoogleAnalytics::Rails
14 19 GAR = GoogleAnalytics::Rails
15 20
16 21 require 'google-analytics-rails/railtie' if defined?(Rails)
3  lib/google-analytics-rails/railtie.rb
... ... @@ -1,7 +1,8 @@
1 1 require 'google-analytics-rails/view_helpers'
2 2
3 3 module GoogleAnalytics::Rails
4   - class Railtie < Rails::Railtie
  4 + # @private
  5 + class Railtie < ::Rails::Railtie
5 6 initializer "google-analytics-rails" do
6 7 ActionView::Base.send :include, ViewHelpers
7 8 end
9 lib/google-analytics-rails/version.rb
... ... @@ -1,7 +1,6 @@
1   -module Google
2   - module Analytics
3   - module Rails
4   - VERSION = "0.0.1"
5   - end
  1 +module GoogleAnalytics
  2 + module Rails
  3 + # Gem version
  4 + VERSION = "0.0.1"
6 5 end
7 6 end
75 lib/google-analytics-rails/view_helpers.rb
... ... @@ -1,34 +1,99 @@
1 1 require 'google_analytics_tools'
2 2
3 3 module GoogleAnalytics::Rails
  4 + # @example Ecommerce example
  5 + #
  6 + # # create a new transaction
  7 + # analytics_add_transaction(
  8 + # '1234', # order ID - required
  9 + # 'Acme Clothing', # affiliation or store name
  10 + # '11.99', # total - required
  11 + # '1.29', # tax
  12 + # '5', # shipping
  13 + # 'San Jose', # city
  14 + # 'California', # state or province
  15 + # 'USA' # country
  16 + # )
  17 + #
  18 + # # add an item to the transaction
  19 + # analytics_add_item(
  20 + # '1234', # order ID - required
  21 + # 'DD44', # SKU/code - required
  22 + # 'T-Shirt', # product name
  23 + # 'Green Medium', # category or variation
  24 + # '11.99', # unit price - required
  25 + # '1' # quantity - required
  26 + # )
  27 + #
  28 + # # submit the transaction
  29 + # analytics_track_transaction
  30 + #
4 31 module ViewHelpers
5   - def analytics_init(*additionalEvents)
  32 + # Initializes the Analytics javascript. Put it in the `<head>` tag.
  33 + #
  34 + # @param events [Array]
  35 + # The page load times and page views are tracked by default, additional events can be added here.
  36 + # @param options [Hash]
  37 + # @option options [Boolean] :local (false) Sets the local development mode.
  38 + # See http://www.google.com/support/forum/p/Google%20Analytics/thread?tid=741739888e14c07a&hl=en
  39 + #
  40 + # @example Set the local bit in development mode
  41 + # analytics_init :local => Rails.env.development?
  42 + #
  43 + # @example Allow links across domains
  44 + # analytics_init [GAQ::Events::SetAllowLinker.new(true)]
  45 + #
  46 + # @return [String] a `<script>` tag, containing the analytics initialization sequence.
  47 + #
  48 + def analytics_init(events = [], options = {})
6 49 raise ArgumentError, "Tracker must be set! Did you set GAR.tracker ?" unless GAR.valid_tracker?
7 50
  51 + local = options.delete(:local) || false
  52 +
8 53 queue = GAQ.new
9   - queue << GAQ::Events::SetAccount.new(GAR.tracker)
10   - queue << GAQ::Events::TrackPageview.new
11   - queue << GAQ::Events::TrackPageLoadTime.new
12 54
13   - additionalEvents.each do |event|
  55 + # unshift => reverse order
  56 + events.unshift GAQ::Events::TrackPageLoadTime.new
  57 + events.unshift GAQ::Events::TrackPageview.new
  58 + events.unshift GAQ::Events::SetAccount.new(GAR.tracker)
  59 +
  60 + if local
  61 + events.push GAQ::Events::SetDomainName.new('none')
  62 + events.push GAQ::Events::SetAllowLinker.new(true)
  63 + end
  64 +
  65 + events.each do |event|
14 66 queue << event
15 67 end
16 68
17 69 queue.to_s.html_safe
18 70 end
19 71
  72 + # Track a custom event
  73 + # @see http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
  74 + #
  75 + # @example
  76 + #
  77 + # analytics_track_event "Videos", "Play", "Gone With the Wind"
  78 + #
20 79 def analytics_track_event(category, action, label, value)
21 80 analytics_render_event(GAQ::Events::TrackEvent.new(category, action, label, value))
22 81 end
23 82
  83 + # Track an ecommerce transaction
  84 + # @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
24 85 def analytics_add_transaction(order_id, store_name, total, tax, shipping, city, state_or_province, country)
25 86 analytics_render_event(GAQ::Events::ECommerce::AddTransaction.new(order_id, store_name, total, tax, shipping, city, state_or_province, country))
26 87 end
27 88
  89 + # Add an item to the current transaction
  90 + # @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
28 91 def analytics_add_item(order_id, product_id, product_name, product_variation, unit_price, quantity)
29 92 analytics_render_event(GAQ::Events::ECommerce::AddItem.new(order_id, product_id, product_name, product_variation, unit_price, quantity))
30 93 end
31 94
  95 + # Flush the current transaction
  96 + # @see http://code.google.com/apis/analytics/docs/tracking/gaTrackingEcommerce.html
32 97 def analytics_track_transaction
33 98 analytics_render_event(GAQ::Events::ECommerce::TrackTransaction.new)
34 99 end

0 comments on commit a3923a8

Please sign in to comment.
Something went wrong with that request. Please try again.