omniture_client 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/MIT-LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2009 Alexandru Catighera
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,202 @@
1
+ # OmnitureClient
2
+ This gem integrates Omniture SiteCatalyst into your web app.
3
+
4
+ ## Installation
5
+ gem install omniture_client
6
+
7
+ ## Omniture on Rails
8
+ This gem provides the ability to track a Rails application at the controller level. I prefer a completely sever-side implementation as opposed to using Omniture's s_code.js. This gem supports using both; however, the majority of the examples will be for a completely
9
+
10
+ ### File Structure
11
+ You create reporters in 'app/reporters' directory, which correspond to your controllers. The example I will use is a `MoviesController`. Omniture client will first attempt to use `MoviesReporter`. If you have not defined the `MoviesReporter`, then it will use the `BasicReporter`, which is what all controllers that do not have any special functionality should use. If you have not yet defined a `BasicReporter`, then it will use a stub that tracks nothing. You will pretty much always at least want a basic reporter, and for controllers that need to track special events or custom variables you should use a custom reporter.
12
+ rails_project/
13
+ ..../app
14
+ ......../reporters
15
+ ............/movies_reporter.rb
16
+ ............/basic_reporter.rb
17
+ ..../config
18
+ ......../omniture.yml
19
+
20
+
21
+ ### Configuration
22
+ You must configure the Omniture namespace, suite(s), version that you wish to use. Optionally, you can set up aliases to make things easier for yourself when coding reporters. Omniture uses `c1`, `e1`, `r`, etc to represent custom variables, e_vars, the referrer, etc. These param names are obscure and can be hard to remember so you can set up aliases for them. From the example below `movie_title` repersents custom varibale 2 (`c2`).
23
+ # config/omniture.yml
24
+ development:
25
+ base_url: http://102.112.2O7.net
26
+ ssl_url: https://102.112.2O7.net
27
+ suite: suite1
28
+ version: G.4--NS
29
+ aliases:
30
+ search_term: c1
31
+ movie_titles: e1
32
+ referrer: r
33
+
34
+ As well as the omniture.yml you need to also add the `app/reporters` directory to your load path. So in your enviroment file add the 2 following lines:
35
+ # config/enviroment.rb
36
+ Rails::Initializer.run do |config|
37
+ config.load_paths += %W( #{RAILS_ROOT}/app/reporters )
38
+ config.gem "omniture_client"
39
+ end
40
+
41
+ ### View
42
+ The view code is straight foward. This is img tag is what actually sends the information to Omniture. It should probably be the last element in the body tag.
43
+
44
+ # app/views/layouts/application.html.erb
45
+ <html>
46
+ <head>...</head>
47
+ <body>
48
+ ....
49
+ <img style='display:none' src="<%= @controller.omniture_url %>">
50
+ </body>
51
+ </html>
52
+
53
+ ### Controller
54
+ Here is an example of a controller we would like to track:
55
+ # app/controller/movies_controller.rb
56
+ class MoviesController < ApplicationController
57
+ def index
58
+ @movies = Movie.all
59
+ end
60
+
61
+ def show
62
+ @movie = Movie.find(params[:id])
63
+ end
64
+
65
+ def search
66
+ @keyword = params[:keyword]
67
+ @movies = Movie.search(@keyword)
68
+ end
69
+ end
70
+
71
+ ### Reporter
72
+ Reporters have access to all controller instance variables and methods. Below we use the `@movie` instance variable and `request` instance method which are from the controller instance. Also, be aware of the omniture.yml configuration, `:search_term` and `movie_titles` are aliases specific to this movies example. In your app you will want to use `c1` and `c2` or set up your own aliases in your omniture.yml.
73
+
74
+ class MoviesReporter < OmnitureClient::Base
75
+ var :pageName do
76
+ page_name = "Movie Page"
77
+ end
78
+
79
+ # We can use any instance methods from the controller, here we use the `request` method
80
+ var :r do
81
+ request.referrer
82
+ end
83
+
84
+ # trigger event5 if user is doing a search and trigger event6 if movie is R rated
85
+ var :events do
86
+ events = []
87
+ events << 'event5' if action_name == 'search' && @keyword
88
+ events << 'event6' if action_name == 'show' && @movie.rating == "R"
89
+ events
90
+ end
91
+
92
+ var :search_term do
93
+ @keyword if action_name == 'search'
94
+ end
95
+
96
+ # list prop that is delimited by |
97
+ var :movie_titles, '|' do
98
+ @movies.map{ |movie| movie.title } if action_name == 'index'
99
+ end
100
+ end
101
+
102
+ The `pageName` param is an extremely important param, the most important in fact. The `r` param which represents the referrer is also very important. The page name will default the the URL if you do not specify it, which is probably what you do not want. For the purposes of simiplicity I made the page name simple but in reality you will want it to be something like this:
103
+ var :pageName do
104
+ case action_name
105
+ when 'show' : "Movie - #{@movie.title}"
106
+ else 'index' : "Movie List Page"
107
+ end
108
+ end
109
+
110
+ That is it to get the basics up and running with Rails.
111
+
112
+ ## Additional Information
113
+
114
+ ### Server-side events
115
+
116
+ Sometime there is a need to track events or other custom variables on the server side. To do this Omniture provides a SOAP API, however, I preferred an alternative method. I preferred to use Rails' flash to store the custom event or variable and append it to the img tag on the next page load. Here is an example of tracking a successful movie creation as an Omniture event (`event7`).
117
+
118
+ # app/controller/movies_controller.rb
119
+ class MoviesController < ApplicationController
120
+ def new
121
+ @movie = Movie.new
122
+ end
123
+
124
+ def create
125
+ @movie = Movie.new(params[:movie])
126
+ if @movie.save
127
+ flash[:notice] = 'Movie was successfully created.'
128
+ omniture_flash[:events] = 'event7'
129
+ redirect_to(@movie)
130
+ else
131
+ render :action => "new"
132
+ end
133
+ end
134
+ end
135
+
136
+
137
+ ## Omniture Client for Sinatra (or other ruby web framework)
138
+ This gem works with any ruby web framework. However, some of the functionality like server-side tracking is only implemented for Rails. Here is an example using Omniture Client to track a Sinatra app.
139
+ # application.rb
140
+ require 'omniture_client'
141
+
142
+ OmnitureClient.config({
143
+ :base_url => 'http://102.112.2O7.net',
144
+ :ssl_url => 'https://102.112.2O7.net',
145
+ :suite => 'suite1',
146
+ :version=> 'G.4--NS',
147
+ :aliases => {
148
+ 'search_terms' => 'c1'
149
+ }
150
+ })
151
+
152
+ helpers do
153
+ def reporter
154
+ SinatraReporter.new(self)
155
+ end
156
+ end
157
+
158
+ get '/' do
159
+ @title = "home"
160
+ haml :index
161
+ end
162
+
163
+ class SinatraReporter < OmnitureClient::Base
164
+ var :pageName do
165
+ @title
166
+ end
167
+ end
168
+
169
+ You will also need to render the img tag in your views since this is the bit that actually sends the request to Omniture.
170
+
171
+ # view/index.html.haml
172
+ %img{ :style => 'display:none', :src => reporter.url }
173
+
174
+
175
+
176
+ ## Javascript Implementation
177
+ A javascript implementation is slightly more obstrusive; however, there are a few extra reports you get access to in the Omniture web tool (ie. click map). So all the server-side code remains the same except for the the variable names. For example, instead of `c1` we use `s.prop1`. To have a javascript implementation, you also have to remove the img tag from your layout and add the reporter variables to the s_code.js. I will use a RJS for this example but you could can handle it a number of ways.
178
+
179
+ # config/omniture.yml
180
+ development:
181
+ base_url: http://102.112.2O7.net
182
+ ssl_url: https://102.112.2O7.net
183
+ suite: suite1
184
+ version: G.4--NS
185
+ aliases:
186
+ search_term: prop1
187
+ movie_titles: e_var1
188
+
189
+ ### Javascript
190
+ Here are the following script tags you will need. If you includes these DO NOT render the img tag shown in the examples above.
191
+
192
+ <script language="javascript" src="//INSERT-DOMAIN-AND-PATH-TO-CODE-HERE/s_code.js"></script>
193
+ <script language="javascript">
194
+ //<![CDATA[
195
+ <% @controller.reporter.vars.each do |var| %>
196
+ s.<%= var.name %>="<%= var.value %>";
197
+ <% end %>
198
+ s.t();
199
+ //]]>
200
+ </script>
201
+
202
+ ##### Copyright (c) 2009 Alexandru Catighera, released under MIT license
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -1,42 +1,24 @@
1
- module OmnitureClient
2
- def import_aliases(alias_hash)
3
- @alias_hash = alias_hash
4
- end
5
-
6
- attr_reader :alias_hash
7
-
8
- module ControllerMethods
9
- def self.included(base)
10
- base.extend(ClassMethods)
11
- end
1
+ require File.dirname(__FILE__) + '/omniture_client/printer'
2
+ require File.dirname(__FILE__) + '/omniture_client/base'
3
+ require File.dirname(__FILE__) + '/omniture_client/var'
4
+ require File.dirname(__FILE__) + '/omniture_client/meta_var'
5
+ require File.dirname(__FILE__) + '/omniture_client/controller_methods'
6
+ require 'cgi'
12
7
 
13
- module ClassMethods
14
- def report_to(reporter)
15
- include InstanceMethods
16
- @reporter = reporter
17
- end
8
+ class BasicReporter < OmnitureClient::Base; end
18
9
 
19
- def reporter
20
- @reporter
21
- end
22
- end
23
-
24
- module InstanceMethods
25
- def reporter
26
- if self.class.reporter.is_a?(Class)
27
- @reporter ||= reporter
28
- elsif self.class.reporter.is_a?(Proc)
29
- @reporter ||= reporter.call
30
- end
31
- end
32
-
33
- def omniture_js
34
- reporter.to_js
35
- end
10
+ module OmnitureClient
11
+ class << self
12
+ attr_accessor :aliases, :base_url, :ssl_url, :suite, :version
36
13
 
37
- def omniture_url
38
- reporter.to_query
14
+ def config(config_hash)
15
+ config_hash.each do |key, val|
16
+ send("#{key}=", val)
39
17
  end
40
18
  end
41
19
  end
42
20
  end
21
+
22
+ if defined?(Rails)
23
+ require File.dirname(__FILE__) + '/rails'
24
+ end
@@ -5,7 +5,7 @@ module OmnitureClient
5
5
 
6
6
  attr_reader :controller
7
7
 
8
- @var_groups = []
8
+ @meta_vars = []
9
9
 
10
10
  def initialize(controller)
11
11
  @controller = controller
@@ -16,24 +16,28 @@ module OmnitureClient
16
16
  end
17
17
 
18
18
  def vars
19
- @vars ||= self.class.var_groups.inject([]) do |vars, grp|
20
- vars << grp.values(controller) if grp
19
+ meta_vars = self.class.meta_vars || []
20
+ @vars ||= meta_vars.inject([]) do |vars, meta_var|
21
+ vars << meta_var.value(controller) if meta_var
21
22
  vars
22
23
  end
23
24
  end
24
25
 
25
26
  def add_var(name, value)
26
- vars << Var.new(name, value)
27
+ self.class.var(name) do
28
+ value
29
+ end
27
30
  end
28
31
 
29
32
  class << self
30
- attr_reader :var_groups
31
-
32
- def define_var(name, &block)
33
- var_group = instance_eval("@#{name} ||= VarGroup.new(name)")
34
- var_group.add_var(block)
35
- var_groups << var_group unless var_groups.include?(var_group)
36
- var_group
33
+ attr_reader :meta_vars
34
+
35
+ def var(name, delimiter = ',', &block)
36
+ @meta_vars ||= []
37
+ meta_var = instance_eval("@#{name} ||= OmnitureClient::MetaVar.new('#{name}', '#{delimiter}')")
38
+ meta_var.add_var(block)
39
+ meta_vars << meta_var unless meta_vars.include?(meta_var)
40
+ meta_var
37
41
  end
38
42
  end
39
43
  end
@@ -0,0 +1,13 @@
1
+ module OmnitureClient
2
+ module ControllerMethods
3
+ attr_accessor :report
4
+
5
+ def omniture_js
6
+ reporter.to_js
7
+ end
8
+
9
+ def omniture_url
10
+ reporter.to_query
11
+ end
12
+ end
13
+ end
@@ -1,12 +1,12 @@
1
1
  module OmnitureClient
2
- class VarGroup
3
- attr_reader :name
2
+ class MetaVar
3
+ attr_reader :name, :delimiter
4
4
  attr_accessor :value_procs
5
5
 
6
- def initialize(name)
6
+ def initialize(name, delimiter)
7
7
  @name = name
8
8
  @value_procs = []
9
- @composite = composite
9
+ @delimiter = delimiter
10
10
  end
11
11
 
12
12
  def add_var(value_proc)
@@ -14,11 +14,7 @@ module OmnitureClient
14
14
  end
15
15
 
16
16
  def value(scope)
17
- Var.new(name, value_procs.map{ |p| scope.instance_eval(p) })
17
+ Var.new(name, value_procs.map{ |p| scope.instance_eval(&p) }.flatten.uniq.join(delimiter))
18
18
  end
19
-
20
- private
21
-
22
- attr_reader :composite
23
19
  end
24
20
  end
@@ -1,30 +1,23 @@
1
1
  module OmnitureClient
2
2
  module Printer
3
- def to_js
4
- var_types.inject("") do |js, var_type|
5
- send(var_type).each do |var|
6
- js << var_to_js(var)
7
- end
8
- end
3
+
4
+ def url(ssl = false)
5
+ suite = OmnitureClient::suite.is_a?(Array) ? OmnitureClient::suite.join(',') : OmnitureClient::suite
6
+ base_url = ssl == :ssl ? OmnitureClient::ssl_url : OmnitureClient::base_url
7
+ "#{base_url}/b/ss/#{suite}/#{OmnitureClient::version}/#{rand(9999999)}?#{query}"
9
8
  end
10
9
 
11
- def to_query
12
- var_types.inject("") do |query, var_type|
13
- send(var_type).each do |var|
14
- query << var_to_query(var)
15
- end
16
- end
10
+ def query
11
+ vars.inject([]) do |query, var|
12
+ query << var_to_query(var) if var.value && var.value != ""
13
+ query
14
+ end.join('&')
17
15
  end
18
16
 
19
17
  private
20
18
 
21
19
  def var_to_query(var)
22
- send(var).to_query(var) + '&' if send(var)
23
- end
24
-
25
- def var_to_js(var)
26
- "var #{var} = '#{send(var).gsub()}'; " if send(var)
20
+ "#{ CGI::escape(var.name) }=#{ CGI::escape(var.value) }" if var
27
21
  end
28
-
29
22
  end
30
23
  end
@@ -2,17 +2,12 @@ module OmnitureClient
2
2
  class Var
3
3
  attr_reader :name, :value
4
4
  def initialize(name, value)
5
- @name = name
6
- if OmnitureClient::alias_hash && OmnitureClient::alias_hash[name]
7
- @name = OmnitureClient::alias_hash[name]
5
+ if OmnitureClient::aliases && OmnitureClient::aliases[name.to_s]
6
+ @name = OmnitureClient::aliases[name.to_s]
8
7
  else
9
- @name = name
8
+ @name = name.to_s
10
9
  end
11
10
  @value = value
12
11
  end
13
-
14
- def to_param
15
- value.to_param(name)
16
- end
17
12
  end
18
13
  end
data/lib/rails.rb CHANGED
@@ -1,31 +1,50 @@
1
1
  module OmnitureClient
2
2
  module ActionControllerMethods
3
- def reports_to_omniture
4
- include ControllerMethods
5
3
 
6
- before_filter do
7
- omniture_flash.each do |name, value|
8
- reporter.add_var(name, value)
9
- end
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def reports_to_omniture
10
+ include InstanceMethods
11
+ before_filter :set_reporter, :assign_flash_vars
12
+ attr_accessor :reporter
10
13
  end
14
+ end
11
15
 
12
- report_to lambda {
13
- begin
14
- "#{controller_path.classify}Reporter".constantize.new(self)
15
- rescue
16
- BasicReporter.new(self)
17
- end
18
- }
19
-
20
- define_method :omniture_flash do
16
+ module InstanceMethods
17
+ def omniture_flash
21
18
  flash[:omniture] ||= {}
22
19
  end
20
+
21
+ def omniture_url
22
+ ssl = :ssl if request.ssl? && OmnitureClient::ssl_url
23
+ reporter.url(ssl)
24
+ end
25
+
26
+ private
27
+
28
+ def set_reporter
29
+ @reporter ||= begin
30
+ "#{controller_path.classify.pluralize}Reporter".constantize.new(self)
31
+ rescue NameError
32
+ BasicReporter.new(self)
33
+ end
34
+ end
35
+
36
+ def assign_flash_vars
37
+ omniture_flash.each do |name, value|
38
+ reporter.add_var(name, value)
39
+ end
40
+ end
23
41
  end
24
42
  end
25
43
  end
26
44
 
27
- if is_defined?(ApplicationController)
28
- ApplicationController.extend(ActionControllerMethods)
29
- ApplicationController.reports_to_omniture
45
+ if defined?(ActionController::Base)
46
+ ActionController::Base.send(:include, OmnitureClient::ActionControllerMethods)
47
+ ApplicationController.reports_to_omniture
30
48
  end
31
49
 
50
+ OmnitureClient::config(YAML::load(File.open(File.join(RAILS_ROOT, 'config', 'omniture.yml')))[RAILS_ENV]) if File.exists?(File.join(RAILS_ROOT, 'config', 'omniture.yml'))
@@ -5,27 +5,30 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{omniture_client}
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Alexandru Catighera"]
12
- s.date = %q{2009-12-02}
12
+ s.date = %q{2010-01-08}
13
13
  s.email = %q{alex@patch.com}
14
14
  s.extra_rdoc_files = [
15
- "README"
15
+ "README.markdown"
16
16
  ]
17
17
  s.files = [
18
- "README",
18
+ ".gitignore",
19
+ "MIT-LICENSE",
20
+ "README.markdown",
19
21
  "Rakefile",
20
22
  "VERSION",
21
23
  "lib/omniture_client.rb",
22
24
  "lib/omniture_client/base.rb",
25
+ "lib/omniture_client/controller_methods.rb",
26
+ "lib/omniture_client/meta_var.rb",
23
27
  "lib/omniture_client/printer.rb",
24
28
  "lib/omniture_client/var.rb",
25
- "lib/omniture_client/var_group.rb",
26
29
  "lib/rails.rb",
27
30
  "omniture_client.gemspec",
28
- "omniture_client.rb"
31
+ "sample/omniture.yml"
29
32
  ]
30
33
  s.homepage = %q{http://github.com/acatighera/omniture_client}
31
34
  s.rdoc_options = ["--charset=UTF-8"]
@@ -0,0 +1,47 @@
1
+ development:
2
+ base_url: http://102.112.2O7.net
3
+ ssl_url: https://102.112.2O7.net
4
+ suite: suite1
5
+ version: G.4--NS
6
+ aliases:
7
+ prop1: c1
8
+ prop2: c2
9
+ prop3: c3
10
+ prop4: c4
11
+ prop5: c5
12
+ prop6: c6
13
+ prop7: c7
14
+ prop8: c8
15
+ prop9: c9
16
+ prop10: c10
17
+ prop11: c11
18
+ prop12: c12
19
+ prop13: c13
20
+ prop14: c14
21
+ prop15: c15
22
+ prop16: c16
23
+ prop17: c17
24
+ prop18: c18
25
+ prop19: c19
26
+ prop20: c20
27
+
28
+ e_var1: v1
29
+ e_var2: v2
30
+ e_var3: v3
31
+ e_var4: v4
32
+ e_var5: v5
33
+ e_var6: v6
34
+ e_var7: v7
35
+ e_var8: v8
36
+ e_var9: v9
37
+ e_var10: v10
38
+ e_var11: v11
39
+ e_var12: v12
40
+ e_var13: v13
41
+ e_var14: v14
42
+ e_var15: v15
43
+ e_var16: v16
44
+ e_var17: v17
45
+ e_var18: v18
46
+ e_var19: v19
47
+ e_var20: v20
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniture_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandru Catighera
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-02 00:00:00 -05:00
12
+ date: 2010-01-08 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -20,19 +20,22 @@ executables: []
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README
23
+ - README.markdown
24
24
  files:
25
- - README
25
+ - .gitignore
26
+ - MIT-LICENSE
27
+ - README.markdown
26
28
  - Rakefile
27
29
  - VERSION
28
30
  - lib/omniture_client.rb
29
31
  - lib/omniture_client/base.rb
32
+ - lib/omniture_client/controller_methods.rb
33
+ - lib/omniture_client/meta_var.rb
30
34
  - lib/omniture_client/printer.rb
31
35
  - lib/omniture_client/var.rb
32
- - lib/omniture_client/var_group.rb
33
36
  - lib/rails.rb
34
37
  - omniture_client.gemspec
35
- - omniture_client.rb
38
+ - sample/omniture.yml
36
39
  has_rdoc: true
37
40
  homepage: http://github.com/acatighera/omniture_client
38
41
  licenses: []
data/README DELETED
File without changes
data/omniture_client.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'lib/omniture_client/base.rb'
2
- require 'lib/omniture_client.rb'
3
- if is_defined?(Rails)
4
- require 'lib/rails.rb'
5
- end
6
-
7
- class BasicReporter; end