staccato 0.0.3 → 0.0.4

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.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## Staccato 0.0.4 ##
2
+
3
+ * adds session start/end controls
4
+ * adds custom dimensions and metrics
5
+
6
+ *Tony Pitale*
7
+
8
+ ## Staccato 0.0.3 ##
9
+
10
+ * adds YARD documentation
11
+ * adds context experiment global options
12
+ * adds language and encoding global options
13
+ * adds referrer global option
14
+
15
+ *Tony Pitale*
16
+
1
17
  ## Staccato 0.0.2 ##
2
18
 
3
19
  * adds timing tracking with block timing
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Ruby Google Analytics Measurement
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/staccato.png)](http://badge.fury.io/rb/staccato)
5
6
  [![Build Status](https://travis-ci.org/tpitale/staccato.png?branch=master)](https://travis-ci.org/tpitale/staccato)
6
7
  [![Code Climate](https://codeclimate.com/github/tpitale/staccato.png)](https://codeclimate.com/github/tpitale/staccato)
7
8
 
@@ -21,70 +22,113 @@ Or install it yourself as:
21
22
 
22
23
  ## Usage ##
23
24
 
24
- tracker = Staccato.tracker('UA-XXXX-Y') # REQUIRED, your Google Analytics Tracking ID
25
+ ```ruby
26
+ tracker = Staccato.tracker('UA-XXXX-Y') # REQUIRED, your Google Analytics Tracking ID
27
+ ```
25
28
 
26
29
  `#tracker` optionally takes a second param for the `client_id` value
27
30
  By default, the `client_id` is set to a random UUID with `SecureRandom.uuid`
28
31
 
29
32
  ### Track some data ###
30
33
 
31
- # Track a Pageview (all values optional)
32
- tracker.pageview(path: '/page-path', hostname: 'mysite.com', title: 'A Page!')
33
-
34
- # Track an Event (all values optional)
35
- tracker.event(category: 'video', action: 'play', label: 'cars', value: 1)
36
-
37
- # Track social activity (all values REQUIRED)
38
- tracker.social(action: 'like', network: 'facebook', target: '/something')
39
-
40
- # Track exceptions (all values optional)
41
- tracker.exception(description: 'RuntimeException', fatal: true)
42
-
43
- # Track timing (all values optional, but should include time)
44
- tracker.timing(category: 'runtime', variable: 'db', label: 'query', time: 50) # time in milliseconds
45
-
46
- tracker.timing(category: 'runtime', variable: 'db', label: 'query') do
47
- some_code_here
48
- end
49
-
50
- # Track transaction (transaction_id REQUIRED)
51
- tracker.transaction({
52
- transaction_id: 12345,
53
- affiliation: 'clothing',
54
- revenue: 17.98,
55
- shipping: 2.00,
56
- tax: 2.50,
57
- currency: 'EUR'
58
- })
59
-
60
- # Track transaction item (matching transaction_id and item name REQUIRED)
61
- tracker.transaction_item({
62
- transaction_id: 12345,
63
- name: 'Shirt',
64
- price: 8.99,
65
- quantity: 2,
66
- code: 'afhcka1230',
67
- variation: 'red',
68
- currency: 'EUR'
69
- })
34
+ ```ruby
35
+ # Track a Pageview (all values optional)
36
+ tracker.pageview(path: '/page-path', hostname: 'mysite.com', title: 'A Page!')
37
+
38
+ # Track an Event (all values optional)
39
+ tracker.event(category: 'video', action: 'play', label: 'cars', value: 1)
40
+
41
+ # Track social activity (all values REQUIRED)
42
+ tracker.social(action: 'like', network: 'facebook', target: '/something')
43
+
44
+ # Track exceptions (all values optional)
45
+ tracker.exception(description: 'RuntimeException', fatal: true)
46
+
47
+ # Track timing (all values optional, but should include time)
48
+ tracker.timing(category: 'runtime', variable: 'db', label: 'query', time: 50) # time in milliseconds
49
+
50
+ tracker.timing(category: 'runtime', variable: 'db', label: 'query') do
51
+ some_code_here
52
+ end
53
+
54
+ # Track transaction (transaction_id REQUIRED)
55
+ tracker.transaction({
56
+ transaction_id: 12345,
57
+ affiliation: 'clothing',
58
+ revenue: 17.98,
59
+ shipping: 2.00,
60
+ tax: 2.50,
61
+ currency: 'EUR'
62
+ })
63
+
64
+ # Track transaction item (matching transaction_id and item name REQUIRED)
65
+ tracker.transaction_item({
66
+ transaction_id: 12345,
67
+ name: 'Shirt',
68
+ price: 8.99,
69
+ quantity: 2,
70
+ code: 'afhcka1230',
71
+ variation: 'red',
72
+ currency: 'EUR'
73
+ })
74
+ ```
75
+
76
+ Each one of these methods returns a particular `hit` type. To send the tracking information to google analytics, simply call `track!`.
77
+
78
+ ```ruby
79
+ tracker.pageview(path: '/item-120291').track!
80
+ ```
70
81
 
71
82
  ### "Global" Options ###
72
83
 
73
- # Track a Non-Interactive Event
74
- tracker.event(category: 'video', action: 'play', non_interactive: true)
84
+ #### Custom Dimensions and Metrics ####
85
+
86
+ ```ruby
87
+ hit = tracker.pageview('/sports-page-5')
88
+ hit.add_custom_dimension(19, 'Sports')
89
+ hit.add_custom_metric(2, 5)
90
+ hit.track!
91
+ ```
92
+
93
+ The first argument is the slot position. Custom dimensions and metrics have 20 slots or 200 if you're "Premium" account.
94
+
95
+ The second argument is the value. For dimensions, that's a text value. For metrics it is an integer.
96
+
97
+ #### Non-Interactive Hit ####
98
+
99
+ ```ruby
100
+ # Track a Non-Interactive Hit
101
+ tracker.event(category: 'video', action: 'play', non_interactive: true)
102
+ ```
75
103
 
76
104
  Non-Interactive events are useful for tracking things like emails sent, or other
77
105
  events that are not directly the result of a user's interaction.
78
106
 
79
107
  The option `non_interactive` is accepted for all methods on `tracker`.
80
108
 
81
- # Tracking an Experiment
82
- # useful for tracking A/B or Multivariate testing
83
- tracker.pageview({
84
- path: '/blog',
85
- experiment_id: 'a7a8d91df',
86
- experiment_variant: 'a'
87
- })
109
+ #### Session Control ####
110
+
111
+ ```ruby
112
+ # start a session
113
+ tracker.pageview(path: '/blog', start_session: true)
114
+
115
+ # end a session
116
+ tracker.pageview(path: '/blog', end_session: true)
117
+ ```
118
+
119
+ Other options are acceptable to start and end a session: `session_start`, `sessoin_end`, and `stop_session`.
120
+
121
+ #### Content Experiment ####
122
+
123
+ ```ruby
124
+ # Tracking an Experiment
125
+ # useful for tracking A/B or Multivariate testing
126
+ tracker.pageview({
127
+ path: '/blog',
128
+ experiment_id: 'a7a8d91df',
129
+ experiment_variant: 'a'
130
+ })
131
+ ```
88
132
 
89
133
  ## Google Documentation
90
134
 
data/lib/staccato/hit.rb CHANGED
@@ -34,7 +34,12 @@ module Staccato
34
34
 
35
35
  # collects the parameters from options for this hit type
36
36
  def params
37
- base_params.merge(global_options_params).merge(hit_params).reject {|_,v| v.nil?}
37
+ base_params.
38
+ merge(global_options_params).
39
+ merge(hit_params).
40
+ merge(custom_dimensions).
41
+ merge(custom_metrics).
42
+ reject {|_,v| v.nil?}
38
43
  end
39
44
 
40
45
  # is this a non interactive hit
@@ -43,6 +48,31 @@ module Staccato
43
48
  1 if options[:non_interactive] # defaults to nil
44
49
  end
45
50
 
51
+ def add_custom_dimension(position, value)
52
+ self.custom_dimensions["cd#{position}"] = value
53
+ end
54
+
55
+ def custom_dimensions
56
+ @custom_dimensions ||= {}
57
+ end
58
+
59
+ def add_custom_metric(position, value)
60
+ self.custom_metrics["cm#{position}"] = value
61
+ end
62
+
63
+ def custom_metrics
64
+ @custom_metrics ||= {}
65
+ end
66
+
67
+ def session_control
68
+ case
69
+ when options[:session_start], options[:start_session]
70
+ 'start'
71
+ when options[:session_end], options[:end_session], options[:stop_session]
72
+ 'end'
73
+ end
74
+ end
75
+
46
76
  # post the hit to GA collection endpoint
47
77
  # @return [Net::HTTPOK] the GA api always returns 200 OK
48
78
  def track!
@@ -62,6 +92,7 @@ module Staccato
62
92
  'tid' => tracker.id,
63
93
  'cid' => tracker.client_id,
64
94
  'ni' => non_interactive,
95
+ 'sc' => session_control,
65
96
  't' => type.to_s
66
97
  }
67
98
  end
@@ -1,4 +1,4 @@
1
1
  module Staccato
2
2
  # The current Staccato VERSION
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
@@ -66,7 +66,7 @@ describe Staccato::Pageview do
66
66
  Staccato::Pageview.new(tracker, {})
67
67
  end
68
68
 
69
- it 'has require params' do
69
+ it 'has required params' do
70
70
  pageview.params.should eq({
71
71
  'v' => 1,
72
72
  'tid' => 'UA-XXXX-Y',
@@ -84,7 +84,7 @@ describe Staccato::Pageview do
84
84
  })
85
85
  end
86
86
 
87
- it 'has require params' do
87
+ it 'has experiment id and variant' do
88
88
  pageview.params.should eq({
89
89
  'v' => 1,
90
90
  'tid' => 'UA-XXXX-Y',
@@ -95,4 +95,66 @@ describe Staccato::Pageview do
95
95
  })
96
96
  end
97
97
  end
98
+
99
+ context "with session control" do
100
+ let(:pageview) do
101
+ Staccato::Pageview.new(tracker, {
102
+ session_start: true
103
+ })
104
+ end
105
+
106
+ it 'has session control param' do
107
+ pageview.params.should eq({
108
+ 'v' => 1,
109
+ 'tid' => 'UA-XXXX-Y',
110
+ 'cid' => '555',
111
+ 't' => 'pageview',
112
+ 'sc' => 'start'
113
+ })
114
+ end
115
+ end
116
+
117
+ context "with some custom dimensions" do
118
+ let(:pageview) do
119
+ Staccato::Pageview.new(tracker)
120
+ end
121
+
122
+ before(:each) do
123
+ pageview.add_custom_dimension(19, 'Apple')
124
+ pageview.add_custom_dimension(8, 'Samsung')
125
+ end
126
+
127
+ it 'has custom dimensions' do
128
+ pageview.params.should eq({
129
+ 'v' => 1,
130
+ 'tid' => 'UA-XXXX-Y',
131
+ 'cid' => '555',
132
+ 't' => 'pageview',
133
+ 'cd19' => 'Apple',
134
+ 'cd8' => 'Samsung'
135
+ })
136
+ end
137
+ end
138
+
139
+ context "with some custom metrics" do
140
+ let(:pageview) do
141
+ Staccato::Pageview.new(tracker)
142
+ end
143
+
144
+ before(:each) do
145
+ pageview.add_custom_metric(12, 42)
146
+ pageview.add_custom_metric(1, 11)
147
+ end
148
+
149
+ it 'has custom dimensions' do
150
+ pageview.params.should eq({
151
+ 'v' => 1,
152
+ 'tid' => 'UA-XXXX-Y',
153
+ 'cid' => '555',
154
+ 't' => 'pageview',
155
+ 'cm12' => 42,
156
+ 'cm1' => 11
157
+ })
158
+ end
159
+ end
98
160
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: staccato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-21 00:00:00.000000000 Z
12
+ date: 2014-01-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler