sfanalytics 0.2.1 → 0.2.13
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +41 -0
- data/VERSION +1 -1
- data/init.rb +1 -3
- data/lib/sfanalytics/view_helpers.rb +19 -0
- data/lib/sfanalytics.rb +57 -51
- data/sfanalytics.gemspec +3 -2
- data/test/sfanalytics_spec.rb +96 -94
- metadata +4 -3
data/README.rdoc
CHANGED
@@ -2,6 +2,47 @@
|
|
2
2
|
|
3
3
|
SFanalytics provides Google Analytics for your app. Probably only really useful for Ecommerce users of analytics, to provide the functionality to add the order items to your transactions for tracking in Analytics.
|
4
4
|
|
5
|
+
== Usage
|
6
|
+
SFanalytics provides a set of helpers that provide the standard and ecommerce functionality of Google Analytics.
|
7
|
+
|
8
|
+
A simple example of its usage is:
|
9
|
+
<%-
|
10
|
+
analytics_add_transaction( {
|
11
|
+
:id => 'A01',
|
12
|
+
:store_name => 'TEST STORE',
|
13
|
+
:total => '3.54',
|
14
|
+
:vat => '0.34',
|
15
|
+
:postage => '2.20',
|
16
|
+
:city => 'Test City',
|
17
|
+
:county => 'Test County',
|
18
|
+
:country => 'Test Kingdom'
|
19
|
+
}, 'UA-1234567-1'
|
20
|
+
)
|
21
|
+
|
22
|
+
analytics_add_item( {
|
23
|
+
:sku => 'TEST123',
|
24
|
+
:name => 'Test Item',
|
25
|
+
:category => 'Test Categoru',
|
26
|
+
:price => '1.99',
|
27
|
+
:quantity => '2'
|
28
|
+
}, 'UA-1234567-1'
|
29
|
+
)
|
30
|
+
|
31
|
+
analytics_add_item( {
|
32
|
+
:sku => 'TEST124',
|
33
|
+
:name => 'Test Item3',
|
34
|
+
:category => 'Test Categoru',
|
35
|
+
:price => '1.99',
|
36
|
+
:quantity => '3'
|
37
|
+
}, 'UA-1234567-2'
|
38
|
+
)
|
39
|
+
|
40
|
+
%>
|
41
|
+
|
42
|
+
<%= analytics_generate('UA-1234566-1') %>
|
43
|
+
|
44
|
+
This adds a transaction, then some line items to the transaction, then outputs the analytics code when analytics_generate is called. Technically the analytics ID is only required on the first helper call, but it does no harm to include it on each call.
|
45
|
+
|
5
46
|
== Note on Patches/Pull Requests
|
6
47
|
|
7
48
|
* Fork the project.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.13
|
data/init.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
module SFanalytics
|
2
|
+
module ViewHelpers
|
3
|
+
attr_reader :analytics
|
4
|
+
def analytics_generate(analytics_id = '')
|
5
|
+
@analytics = SFanalytics::SFa::new(analytics_id) unless @analytics
|
6
|
+
return @analytics.generate
|
7
|
+
end
|
8
|
+
|
9
|
+
def analytics_add_transaction(transaction, analytics_id = '')
|
10
|
+
@analytics = SFanalytics::SFa::new(analytics_id) unless @analytics
|
11
|
+
return @analytics.add_transaction(transaction)
|
12
|
+
end
|
13
|
+
|
14
|
+
def analytics_add_item(item, analytics_id = '')
|
15
|
+
@analytics = SFanalytics::SFa::new(analytics_id) unless @analytics
|
16
|
+
return @analytics.add_line_item(item)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/sfanalytics.rb
CHANGED
@@ -1,58 +1,64 @@
|
|
1
1
|
require 'haml'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module SFanalytics
|
4
|
+
class SFa
|
5
|
+
attr_reader :analytics_id, :transaction, :line_items, :transaction_id
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
if defined? Rails
|
8
|
+
require 'sfanalytics/view_helpers'
|
9
|
+
ActionView::Base.send :include, ViewHelpers
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(analytics_id)
|
13
|
+
raise ArgumentError.new if ! analytics_id.match(/UA-\d{0,8}-\d{0,3}/)
|
14
|
+
@analytics_id = analytics_id
|
15
|
+
@line_items = ''
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate
|
19
|
+
template = File.read(File.dirname(__FILE__) + '/../templates/analytics.html.haml')
|
11
20
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
:
|
22
|
-
|
23
|
-
|
24
|
-
obj = Object.new
|
25
|
-
Haml::Engine.new(template).def_method(obj, :render, :analytics_id)
|
26
|
-
return obj.render(:analytics_id => "Hello!")
|
27
|
-
end
|
21
|
+
obj = Object.new
|
22
|
+
Haml::Engine.new(template).def_method(obj, :render, :analytics_id, :analytics_transaction, :line_items)
|
23
|
+
return obj.render(
|
24
|
+
:analytics_id => @analytics_id,
|
25
|
+
:analytics_transaction => @transaction,
|
26
|
+
:line_items => @line_items
|
27
|
+
)
|
28
|
+
|
29
|
+
obj = Object.new
|
30
|
+
Haml::Engine.new(template).def_method(obj, :render, :analytics_id)
|
31
|
+
return obj.render(:analytics_id => "Hello!")
|
32
|
+
end
|
28
33
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
34
|
+
def add_transaction(transaction)
|
35
|
+
|
36
|
+
template = File.read(File.dirname(__FILE__) + '/../templates/transaction.html.haml')
|
37
|
+
|
38
|
+
obj = Object.new
|
39
|
+
Haml::Engine.new(template).def_method(obj, :render, :analytics_id, :transaction)
|
40
|
+
@transaction = obj.render(
|
41
|
+
:analytics_id => @analytics_id,
|
42
|
+
:transaction => transaction
|
43
|
+
)
|
44
|
+
|
45
|
+
@transaction_id = transaction[:id]
|
46
|
+
return true
|
47
|
+
end
|
48
|
+
|
49
|
+
def add_line_item(item)
|
50
|
+
raise "No Transaction Defined" unless(@transaction)
|
51
|
+
|
52
|
+
template = File.read(File.dirname(__FILE__) + '/../templates/line_item.html.haml')
|
53
|
+
|
54
|
+
obj = Object.new
|
55
|
+
Haml::Engine.new(template).def_method(obj, :render, :transaction, :item)
|
56
|
+
@line_items << obj.render(
|
57
|
+
:transaction => @transaction_id,
|
58
|
+
:item => item
|
59
|
+
)
|
43
60
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
template = File.read(File.dirname(__FILE__) + '/../templates/line_item.html.haml')
|
48
|
-
|
49
|
-
obj = Object.new
|
50
|
-
Haml::Engine.new(template).def_method(obj, :render, :transaction, :item)
|
51
|
-
@line_items << obj.render(
|
52
|
-
:transaction => @transaction_id,
|
53
|
-
:item => item
|
54
|
-
)
|
55
|
-
|
56
|
-
return true
|
61
|
+
return true
|
62
|
+
end
|
57
63
|
end
|
58
|
-
end
|
64
|
+
end
|
data/sfanalytics.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sfanalytics}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.13"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Graeme Lawton"]
|
12
|
-
s.date = %q{2010-04
|
12
|
+
s.date = %q{2010-05-04}
|
13
13
|
s.description = %q{
|
14
14
|
SFanalytics provides Google Analytics for your app. Probably only really
|
15
15
|
useful for Ecommerce users of analytics, to provide the functionality to add
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"VERSION",
|
30
30
|
"init.rb",
|
31
31
|
"lib/sfanalytics.rb",
|
32
|
+
"lib/sfanalytics/view_helpers.rb",
|
32
33
|
"sfanalytics.gemspec",
|
33
34
|
"templates/analytics.html.haml",
|
34
35
|
"templates/line_item.html.haml",
|
data/test/sfanalytics_spec.rb
CHANGED
@@ -1,28 +1,29 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
describe SFanalytics
|
3
|
+
describe SFanalytics do
|
4
|
+
describe SFanalytics::SFa, "initialize" do
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
it "should be instantiated with a valid looking analytics id" do
|
7
|
+
lambda { SFanalytics::SFa.new( "UA-1234567-1" ) }.should_not raise_error
|
8
|
+
lambda { SFanalytics::SFa.new() }.should raise_error(ArgumentError)
|
9
|
+
lambda { SFanalytics::SFa.new( "foobar" ) }.should raise_error(ArgumentError)
|
10
|
+
end
|
10
11
|
|
11
|
-
end
|
12
|
+
end
|
12
13
|
|
13
|
-
describe SFanalytics, "generate" do
|
14
|
+
describe SFanalytics::SFa, "generate" do
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
before(:each) do
|
17
|
+
@sfanalytics = SFanalytics::SFa.new('UA-1234567-1')
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
it "should be able to generate output code" do
|
21
|
+
@sfanalytics.should respond_to(:generate)
|
22
|
+
@sfanalytics.generate.class.should == String
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
25
|
+
it "should generate the correct basic tracking code" do
|
26
|
+
@sfanalytics.generate.should == <<-EOS
|
26
27
|
<script type='text/javascript'>
|
27
28
|
var _gaq = _gaq || [];
|
28
29
|
_gaq.push(['_setAccount', 'UA-1234567-1']);
|
@@ -35,35 +36,35 @@ describe SFanalytics, "generate" do
|
|
35
36
|
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
|
36
37
|
})();
|
37
38
|
</script>
|
38
|
-
|
39
|
-
|
39
|
+
EOS
|
40
|
+
end
|
40
41
|
|
41
|
-
end
|
42
|
-
|
43
|
-
describe SFanalytics, "generate_transaction" do
|
44
|
-
before(:each) do
|
45
|
-
@sfanalytics = SFanalytics.new('UA-1234567-1')
|
46
|
-
@trx_data = {
|
47
|
-
:id => 'A01',
|
48
|
-
:store_name => 'TEST STORE',
|
49
|
-
:total => '3.54',
|
50
|
-
:vat => '0.34',
|
51
|
-
:postage => '2.20',
|
52
|
-
:city => 'Test City',
|
53
|
-
:county => 'Test County',
|
54
|
-
:country => 'Test Kingdom'
|
55
|
-
}
|
56
42
|
end
|
43
|
+
|
44
|
+
describe SFanalytics::SFa, "generate_transaction" do
|
45
|
+
before(:each) do
|
46
|
+
@sfanalytics = SFanalytics::SFa.new('UA-1234567-1')
|
47
|
+
@trx_data = {
|
48
|
+
:id => 'A01',
|
49
|
+
:store_name => 'TEST STORE',
|
50
|
+
:total => '3.54',
|
51
|
+
:vat => '0.34',
|
52
|
+
:postage => '2.20',
|
53
|
+
:city => 'Test City',
|
54
|
+
:county => 'Test County',
|
55
|
+
:country => 'Test Kingdom'
|
56
|
+
}
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
it "should allow a transaction to be added" do
|
60
|
+
@sfanalytics.should respond_to(:add_transaction)
|
61
|
+
lambda {@sfanalytics.add_transaction}.should raise_error(ArgumentError)
|
62
|
+
@sfanalytics.add_transaction(@trx_data).should == true
|
63
|
+
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
65
|
+
it "should generate the correct code with a transaction" do
|
66
|
+
@sfanalytics.add_transaction(@trx_data).should
|
67
|
+
@sfanalytics.generate().should == <<-EOS
|
67
68
|
<script type='text/javascript'>
|
68
69
|
var _gaq = _gaq || [];
|
69
70
|
_gaq.push(['_setAccount', 'UA-1234567-1']);
|
@@ -85,65 +86,66 @@ describe SFanalytics, "generate_transaction" do
|
|
85
86
|
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
|
86
87
|
})();
|
87
88
|
</script>
|
88
|
-
|
89
|
+
EOS
|
90
|
+
end
|
89
91
|
end
|
90
|
-
end
|
91
92
|
|
92
|
-
describe SFanalytics, "Add order items" do
|
93
|
+
describe SFanalytics::SFa, "Add order items" do
|
93
94
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
95
|
+
before(:each) do
|
96
|
+
@sfanalytics = SFanalytics::SFa.new('UA-1234567-1')
|
97
|
+
@sfanalytics.add_transaction({
|
98
|
+
:id => 'A01',
|
99
|
+
:store_name => 'TEST STORE',
|
100
|
+
:total => '3.54',
|
101
|
+
:vat => '0.34',
|
102
|
+
:postage => '2.20',
|
103
|
+
:city => 'Test City',
|
104
|
+
:county => 'Test County',
|
105
|
+
:country => 'Test Kingdom'
|
106
|
+
})
|
107
|
+
@line_item = {
|
108
|
+
:sku => 'TEST123',
|
109
|
+
:name => 'Test Item',
|
110
|
+
:category => 'Test Categoru',
|
111
|
+
:price => '1.99',
|
112
|
+
:quantity => '2'
|
113
|
+
}
|
114
|
+
end
|
114
115
|
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
116
|
+
it "should allow a line_item to be added" do
|
117
|
+
@sfanalytics.should respond_to(:add_line_item)
|
118
|
+
lambda {@sfanalytics.add_line_item}.should raise_error(ArgumentError)
|
119
|
+
@sfanalytics.add_line_item(@line_item).should == true
|
120
|
+
@sfanalytics.generate.should match /_addItem/
|
121
|
+
end
|
121
122
|
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
123
|
+
it "should allow multiple line_items to be added" do
|
124
|
+
@sfanalytics.add_line_item(@line_item)
|
125
|
+
@sfanalytics.add_line_item(@line_item)
|
126
|
+
#Check there are two instances of _addItem
|
127
|
+
@sfanalytics.generate.split(/_addItem/).count.should == 3
|
127
128
|
|
128
|
-
|
129
|
-
|
130
|
-
|
129
|
+
@sfanalytics.generate.split(/TEST123/).count.should == 3
|
130
|
+
@sfanalytics.generate.split(/A01/).count.should == 4
|
131
|
+
end
|
131
132
|
|
132
|
-
end
|
133
|
-
|
134
|
-
describe SFanalytics, "Error Handling" do
|
135
|
-
before (:each) do
|
136
|
-
@line_item = {
|
137
|
-
:sku => 'TEST123',
|
138
|
-
:name => 'Test Item',
|
139
|
-
:category => 'Test Categoru',
|
140
|
-
:price => '1.99',
|
141
|
-
:quantity => '2'
|
142
|
-
}
|
143
133
|
end
|
144
134
|
|
145
|
-
|
146
|
-
|
147
|
-
|
135
|
+
describe SFanalytics::SFa, "Error Handling" do
|
136
|
+
before (:each) do
|
137
|
+
@line_item = {
|
138
|
+
:sku => 'TEST123',
|
139
|
+
:name => 'Test Item',
|
140
|
+
:category => 'Test Categoru',
|
141
|
+
:price => '1.99',
|
142
|
+
:quantity => '2'
|
143
|
+
}
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should fail if you try to add items with no transaction" do
|
147
|
+
sfanalytics = SFanalytics::SFa.new('UA-1234567-1')
|
148
|
+
lambda{sfanalytics.add_line_item(@line_item)}.should raise_error
|
149
|
+
end
|
148
150
|
end
|
149
|
-
end
|
151
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 13
|
9
|
+
version: 0.2.13
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Graeme Lawton
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04
|
17
|
+
date: 2010-05-04 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -47,6 +47,7 @@ files:
|
|
47
47
|
- VERSION
|
48
48
|
- init.rb
|
49
49
|
- lib/sfanalytics.rb
|
50
|
+
- lib/sfanalytics/view_helpers.rb
|
50
51
|
- sfanalytics.gemspec
|
51
52
|
- templates/analytics.html.haml
|
52
53
|
- templates/line_item.html.haml
|