gabba-gmp 0.0.6 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fa1fbbacd4ca5a11831eaa722996264acfa10816
4
- data.tar.gz: 216b0557d338dae6f714eac54dfc3df6570a263a
3
+ metadata.gz: c717601102a85e2882336840bf5b98d1bcbdca3d
4
+ data.tar.gz: bebccac8403b912d503aa4e437c1139256f7e917
5
5
  SHA512:
6
- metadata.gz: 64eace221e2ed9b8d30ef049b6322461c6e01d0e30bb90f7903387c613a50c58d13bdbfa19ee0190935e84093fe1b527ee396a210552ed1064a05699ea787e16
7
- data.tar.gz: 61ec5d5326491020e9408bc807b4054d547b41286657d1ae477938ffbcdd897d89d59f41ccf5e89325f9cb6114be2f6cd1cf6a504a32ba0bbdc96be9b30885f4
6
+ metadata.gz: 5cbf3ea181ff40f6974cb6f4b046e72dbd1adc689ea9ef318c40734d6ea1029d43918412ec6d2f483f346f90fa0c5d8cdc8a6aaf7a1a6831f877bdad6052914e
7
+ data.tar.gz: 5213d1478a2be1488d5027cb8fd3de6840a785b159be639fb80ba01feb341b76e5a17698fcd7823e9ca6183c955f8d1c65a41d66fac161bf14f365282f82c021
data/Readme.md CHANGED
@@ -25,7 +25,7 @@ gabba.page_view(request, "Page Title")
25
25
 
26
26
  ```
27
27
 
28
- Or if you want to get really fancy you can update the parameters for the page view only..
28
+ Or if you want to get really fancy you can update the parameters for this page view only...
29
29
  ```ruby
30
30
  gabba.page_view(request, "Page Title", document_path: "/manually/fiddled/url")
31
31
 
@@ -92,6 +92,61 @@ gabba.event("Videos", "Play", "ID", "123")
92
92
 
93
93
  ```
94
94
 
95
+ ### Parameters that may be set
96
+
97
+ protocol_version:
98
+
99
+ tracking_id:
100
+
101
+ client_id:
102
+
103
+ user_id:
104
+
105
+ user_ip_address:
106
+
107
+ user_agent:
108
+
109
+ user_language:
110
+
111
+ hit_type:
112
+
113
+ document_title:
114
+
115
+ document_path:
116
+
117
+ document_referrer:
118
+
119
+ document_host:
120
+
121
+ event_category:
122
+
123
+ event_action:
124
+
125
+ event_label:
126
+
127
+ event_value:
128
+
129
+ campaign_name:
130
+
131
+ campaign_source:
132
+
133
+ campaign_medium:
134
+
135
+ campaign_keyword:
136
+
137
+ campaign_content:
138
+
139
+ dimension_[1-200]:
140
+
141
+ **Untested**
142
+
143
+ user_screen_resolution:
144
+
145
+ user_viewport_size:
146
+
147
+ user_screen_colors:
148
+
149
+
95
150
  ### License
96
151
 
97
152
  Gabba is released under the [MIT License](http://opensource.org/licenses/MIT).
@@ -4,7 +4,7 @@ module GabbaGMP
4
4
  attr_accessor :name, :source, :medium, :keyword, :content
5
5
 
6
6
  def present?
7
- name.present? or source.present? or medium.present? or keyword.present? or content.present?
7
+ !name.to_s.empty? or !source.to_s.empty? or !medium.to_s.empty? or !keyword.to_s.empty? or !content.to_s.empty?
8
8
  end
9
9
  end
10
10
  end
@@ -14,7 +14,7 @@ module GabbaGMP
14
14
  #
15
15
  # Returns array with the custom variable data
16
16
  def set_custom_var(index, name, value)
17
- raise "Index must be between 1 and #{GabbaGMP::DIMENSION_MAX}" unless (1..GabbaGMP::DIMENSION_MAX).include?(index)
17
+ raise GoogleAnalyticsInvalidParameterError, "Index must be between 1 and #{GabbaGMP::DIMENSION_MAX}" unless (1..GabbaGMP::DIMENSION_MAX).include?(index)
18
18
 
19
19
  @sessionopts["dimension_#{index}".to_sym] = value
20
20
  end
@@ -18,7 +18,8 @@ module GabbaGMP
18
18
  hey(event_params(category, action, label, value, options))
19
19
  end
20
20
 
21
- # Public: Renders event params data in the format needed for GA
21
+ private
22
+ # Private: Renders event params data in the format needed for GA
22
23
  # Called before actually sending the data along to GA in GabbaGMP#event
23
24
  def event_params(category, action, label, value, event_options)
24
25
  options = {
@@ -26,8 +27,8 @@ module GabbaGMP
26
27
  event_category: category,
27
28
  event_action: action
28
29
  }
29
- options[:event_label] = label if label.present?
30
- options[:event_value] = value if value.present?
30
+ options[:event_label] = label if label
31
+ options[:event_value] = value if value
31
32
  @sessionopts.merge(options).merge!(event_options)
32
33
  end
33
34
  end
@@ -33,7 +33,7 @@ module GabbaGMP
33
33
 
34
34
  ESCAPES = %w{ ' ! * ) }
35
35
 
36
- attr_accessor :debug
36
+ attr_accessor :logger
37
37
 
38
38
  # Public: Initialize Gabba Google Analytics Tracking Object.
39
39
  #
@@ -49,10 +49,10 @@ module GabbaGMP
49
49
  #
50
50
  def initialize(ga_tracking_id, request, cookies, options = {})
51
51
  client_id_cookie = options[:client_id_cookie_sym]
52
- client_id_cookie = :utm_visitor_uuid if !client_id_cookie.present? or !client_id_cookie.kind_of? Symbol
52
+ client_id_cookie = :utm_visitor_uuid if client_id_cookie.nil? or !client_id_cookie.kind_of? Symbol
53
53
 
54
- if !cookies[client_id_cookie].present?
55
- cookie_expiry = options[:client_id_cookie_expiry] ? options[:client_id_cookie_expiry] : 1.year.from_now
54
+ if cookies[client_id_cookie].nil?
55
+ cookie_expiry = options[:client_id_cookie_expiry] ? options[:client_id_cookie_expiry] : Time.now + (60*60*24*365)
56
56
  cookies[client_id_cookie] = { value: "#{SecureRandom.uuid}", expires: cookie_expiry}
57
57
  end
58
58
 
@@ -64,16 +64,15 @@ module GabbaGMP
64
64
  user_agent: request.user_agent,
65
65
  user_language: preferred_language(request.accept_language)}
66
66
 
67
- @sessionopts[:document_referrer] = request.referrer if request.referrer.present? and !request.referrer.starts_with?("#{request.protocol}#{request.host_with_port}")
67
+ @sessionopts[:document_referrer] = request.referrer if request.referrer and !request.referrer.start_with?("#{request.protocol}#{request.host_with_port}")
68
68
 
69
- debug = false
70
69
  end
71
70
 
72
71
  def preferred_language(language)
73
- return "" unless language.present?
72
+ return "" unless language
74
73
 
75
74
  language_arr = language.split(",").map {|lang_pref| lang_pref.split(";")}
76
- language_arr[0][0].downcase #just get the first language. Will probably be correct.
75
+ language_arr[0][0].downcase.strip #just get the first language. Will probably be correct.
77
76
  end
78
77
 
79
78
  # Public: Set the session's parameters. This will be added to all actions that are sent to analytics.
@@ -81,7 +80,7 @@ module GabbaGMP
81
80
  # See:: ParameterMap:GA_PARAMS
82
81
  def add_options(options)
83
82
  options.keys.each do |key|
84
- raise GoogleAnalyticsParameterNotFoundError, "Parameter '#{key}'" unless GA_PARAMS[key].present?
83
+ raise GoogleAnalyticsParameterNotFoundError, "Parameter '#{key}'" unless GA_PARAMS[key]
85
84
  end
86
85
 
87
86
  @sessionopts.merge!(options)
@@ -102,23 +101,27 @@ module GabbaGMP
102
101
 
103
102
  @sessionopts[:campaign_medium] = campaign.medium
104
103
  @sessionopts[:campaign_medium] ||= "(none)"
104
+
105
+ @sessionopts.delete(:campaign_keyword)
106
+ @sessionopts[:campaign_keyword] = campaign.keyword if campaign.keyword
105
107
 
106
- @sessionopts[:campaign_keyword] = campaign.keyword if campaign.keyword.present?
107
-
108
- @sessionopts[:campaign_content] = campaign.content if campaign.content.present?
108
+ @sessionopts.delete(:campaign_content)
109
+ @sessionopts[:campaign_content] = campaign.content if campaign.content
109
110
  end
110
111
  end
111
112
  end
112
113
 
114
+
115
+ private
113
116
  # Sanity check that we have needed params to even call GA
114
117
  def validate_session_parameters(params)
115
- raise GoogleAnalyticsRequiredParameterMissingError, "Protocol version is required" unless params[:protocol_version].present?
116
- raise GoogleAnalyticsRequiredParameterMissingError, "Tracking id is required" unless params[:tracking_id].present?
117
- raise GoogleAnalyticsRequiredParameterMissingError, "Client id is required" unless params[:client_id].present?
118
- raise GoogleAnalyticsRequiredParameterMissingError, "Hit type is required" unless params[:hit_type].present?
118
+ raise GoogleAnalyticsRequiredParameterMissingError, "Protocol version is required" unless params[:protocol_version]
119
+ raise GoogleAnalyticsRequiredParameterMissingError, "Tracking id is required" unless params[:tracking_id]
120
+ raise GoogleAnalyticsRequiredParameterMissingError, "Client id is required" unless params[:client_id]
121
+ raise GoogleAnalyticsRequiredParameterMissingError, "Hit type is required" unless params[:hit_type]
119
122
 
120
123
  params.keys.each do |param|
121
- raise GoogleAnalyticsInvalidParameterError, "The parameter '#{param}' is not currently recognised." unless GA_PARAMS[param].present?
124
+ raise GoogleAnalyticsInvalidParameterError, "The parameter '#{param}' is not currently recognised." unless GA_PARAMS[param]
122
125
  end
123
126
  end
124
127
 
@@ -129,10 +132,10 @@ module GabbaGMP
129
132
 
130
133
  @http ||= Net::HTTP::Persistent.new 'GabbaGMP'
131
134
 
132
- Rails.logger.info "GABBA_GMP: request params: #{query}" if debug
135
+ @logger.info "GABBA_GMP: request params: #{query}" if @logger
133
136
 
134
137
  request = Net::HTTP::Get.new("#{BEACON_PATH}?#{query}")
135
- request["User-Agent"] = URI.escape(params[:user_agent])
138
+ request["User-Agent"] = URI.escape(params[:user_agent]) if params[:user_agent]
136
139
  request["Accept"] = "*/*"
137
140
  uri = URI "http://#{GOOGLE_HOST}/#{BEACON_PATH}"
138
141
  response = @http.request(uri, request)
@@ -16,7 +16,8 @@ module GabbaGMP
16
16
  hey(page_view_params(title, request.fullpath, options))
17
17
  end
18
18
 
19
- # Public: Renders the page view params data in the format needed for GA
19
+ private
20
+ # Private: Renders the page view params data in the format needed for GA
20
21
  # Called before actually sending the data along to GA.
21
22
  def page_view_params(title, doc_path, options)
22
23
  @sessionopts.merge({
@@ -1,5 +1,5 @@
1
1
  module GabbaGMP
2
2
  unless const_defined?('VERSION')
3
- VERSION = "0.0.6"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe GabbaGMP::GabbaGMP::Campaign do
4
+ describe "#present?" do
5
+ let(:campaign) { GabbaGMP::GabbaGMP::Campaign.new}
6
+ before do
7
+ campaign.name = "Campaign"
8
+ campaign.source = "Source"
9
+ campaign.medium = "Medium"
10
+ campaign.keyword = "Keyword"
11
+ campaign.content = "Content"
12
+ end
13
+
14
+ it "has variables set" do
15
+ expect(campaign.name).to eq("Campaign")
16
+ expect(campaign.source).to eq("Source")
17
+ expect(campaign.medium).to eq("Medium")
18
+ expect(campaign.keyword).to eq("Keyword")
19
+ expect(campaign.content).to eq("Content")
20
+ end
21
+
22
+ it "is present?" do
23
+ expect(campaign.present?).to be(true)
24
+ end
25
+
26
+ it "is present? because of name" do
27
+ campaign = GabbaGMP::GabbaGMP::Campaign.new
28
+ campaign.name = 'X'
29
+ expect(campaign.present?).to be(true)
30
+ end
31
+
32
+ it "is present? because of source" do
33
+ campaign = GabbaGMP::GabbaGMP::Campaign.new
34
+ campaign.source = 'X'
35
+ expect(campaign.present?).to be(true)
36
+ end
37
+
38
+ it "is present? because of medium" do
39
+ campaign = GabbaGMP::GabbaGMP::Campaign.new
40
+ campaign.medium = 'X'
41
+ expect(campaign.present?).to be(true)
42
+ end
43
+
44
+ it "is present? because of keyword" do
45
+ campaign = GabbaGMP::GabbaGMP::Campaign.new
46
+ campaign.keyword = 'X'
47
+ expect(campaign.present?).to be(true)
48
+ end
49
+
50
+ it "is present? because of content" do
51
+ campaign = GabbaGMP::GabbaGMP::Campaign.new
52
+ campaign.content = 'X'
53
+ expect(campaign.present?).to be(true)
54
+ end
55
+
56
+ it "is not present?" do
57
+ campaign = GabbaGMP::GabbaGMP::Campaign.new
58
+ expect(campaign.present?).to be(false)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe GabbaGMP::GabbaGMP::CustomVars do
5
+ describe "#set_custom_var" do
6
+ let(:request) {MockRequest.new}
7
+ let(:cookies) {MockCookies.new(utm_visitor_uuid: 1234)}
8
+ let(:gabbaGmp) { GabbaGMP::GabbaGMP.new("tracker", request, cookies)}
9
+ before do
10
+ stub_analytics("")
11
+ end
12
+
13
+ #index, name, value
14
+
15
+ it "#set_custom_var(index, name, value) using pageview" do
16
+ gabbaGmp.set_custom_var(1, "Dogs", "Black")
17
+ gabbaGmp.page_view(request)
18
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "", tid: "tracker", cid: "1234", t: "pageview", dp: "", cd1: "Black"}))
19
+
20
+ gabbaGmp.set_custom_var(1, "Dog", "Spotty")
21
+ gabbaGmp.set_custom_var(2, "Lemming", "Fall")
22
+ gabbaGmp.set_custom_var(3, "Kite", "Surf")
23
+ gabbaGmp.set_custom_var(200, "Cabbage", "Patch")
24
+ gabbaGmp.page_view(request)
25
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "", tid: "tracker", cid: "1234", t: "pageview", dp: "",
26
+ cd1: "Spotty", cd2: "Fall", cd3: "Surf", cd200: "Patch"}))
27
+
28
+ expect {gabbaGmp.set_custom_var(201, "Cabbage", "Patch")}.to raise_error(GabbaGMP::GoogleAnalyticsInvalidParameterError)
29
+
30
+ gabbaGmp.page_view(request, nil, dimension_4: "Ni!!")
31
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "", tid: "tracker", cid: "1234", t: "pageview", dp: "",
32
+ cd1: "Spotty", cd2: "Fall", cd3: "Surf", cd200: "Patch", cd4: "Ni!!"}))
33
+ end
34
+
35
+ it "#set_custom_var(index, name, value) using event" do
36
+ gabbaGmp.set_custom_var(1, "Dogs", "Black")
37
+ gabbaGmp.event("Cats", "Action")
38
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
39
+ ec: "Cats", ea: "Action", cd1: "Black"}))
40
+
41
+ gabbaGmp.set_custom_var(1, "Letting type", "Let's Go")
42
+ gabbaGmp.set_custom_var(2, "UFO", "Kite")
43
+ gabbaGmp.set_custom_var(200, "Kite", "Surf")
44
+ gabbaGmp.event("Cats", "Action")
45
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
46
+ ec: "Cats", ea: "Action", cd1: "Let's Go", cd2: "Kite", cd200: "Surf"}))
47
+
48
+ expect {gabbaGmp.set_custom_var(201, "Cabbage", "Patch")}.to raise_error(GabbaGMP::GoogleAnalyticsInvalidParameterError)
49
+
50
+ gabbaGmp.event("Cats", "Action", nil, nil, dimension_3: "NOW!")
51
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
52
+ ec: "Cats", ea: "Action", cd1: "Let's Go", cd2: "Kite", cd200: "Surf", cd3: "NOW!"}))
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe GabbaGMP::GabbaGMP::Event do
5
+ describe "#event" do
6
+ let(:request) {MockRequest.new}
7
+ let(:cookies) {MockCookies.new(utm_visitor_uuid: 1234)}
8
+ let(:gabbaGmp) { GabbaGMP::GabbaGMP.new("tracker", request, cookies)}
9
+ before do
10
+ stub_analytics("")
11
+ end
12
+
13
+ #category, action, label = nil, value = nil, options = {}
14
+
15
+ it "#event(category, action)" do
16
+ gabbaGmp.event("Cats", "Action")
17
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
18
+ ec: "Cats", ea: "Action"}))
19
+ end
20
+
21
+ it "#event(category, action, label)" do
22
+ gabbaGmp.event("Cats", "Action", "Label")
23
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
24
+ ec: "Cats", ea: "Action", el: "Label"}))
25
+ end
26
+
27
+ it "#event(category, action, label, value)" do
28
+ gabbaGmp.event("Cats", "Action", "Label", "Value")
29
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
30
+ ec: "Cats", ea: "Action", el: "Label", ev: "Value"}))
31
+ end
32
+
33
+ it "#event(category, action, label, value, options)" do
34
+ gabbaGmp.event("Cats", "Action", "Label", "Value", user_language: "en-au")
35
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
36
+ ec: "Cats", ea: "Action", el: "Label", ev: "Value", ul: "en-au"}))
37
+ end
38
+
39
+ it "must not interfere with other calls if using options" do
40
+ gabbaGmp.event("Cats", "Action", "Label", "Value", user_language: "en-au")
41
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
42
+ ec: "Cats", ea: "Action", el: "Label", ev: "Value", ul: "en-au"}))
43
+
44
+ gabbaGmp.event("Cats", "Action", "Label", "Value")
45
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
46
+ ec: "Cats", ea: "Action", el: "Label", ev: "Value"}))
47
+
48
+ gabbaGmp.event("Cats", "Action", "Label", "Value", document_path: "/path")
49
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
50
+ ec: "Cats", ea: "Action", el: "Label", ev: "Value", dp: "/path"}))
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,139 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe GabbaGMP::GabbaGMP do
5
+ describe "#initialize" do
6
+ let(:request) {MockRequest.new}
7
+ let(:cookies) {MockCookies.new(utm_visitor_uuid: 1234, visitor_id: 567)}
8
+ before do
9
+ stub_analytics("")
10
+ end
11
+
12
+ #index, name, value
13
+ it "#initialize(ga_tracking_id, request, cookies)" do
14
+ gabba = GabbaGMP::GabbaGMP.new("tracker123", request, cookies)
15
+ gabba.event("Cats", "Action")
16
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker123", cid: "1234", t: "event",
17
+ ec: "Cats", ea: "Action"}))
18
+
19
+ end
20
+
21
+ it "#initialize acceptLanguage" do
22
+ request.accept_language = "da , en-gb;q=0.8, en;q=0.7"
23
+ gabba = GabbaGMP::GabbaGMP.new("tracker123", request, cookies)
24
+ gabba.event("Cats", "Action")
25
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker123", cid: "1234", t: "event",
26
+ ec: "Cats", ea: "Action", ul: "da"}))
27
+ end
28
+
29
+ it "#initialize referrer" do
30
+ request.referrer = "http://www.example.com/my-pages"
31
+ gabba = GabbaGMP::GabbaGMP.new("tracker123", request, cookies)
32
+ gabba.event("Cats", "Action")
33
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker123", cid: "1234", t: "event",
34
+ ec: "Cats", ea: "Action"}))
35
+
36
+ request.referrer = "http://www.otherexample.com/my-pages"
37
+ gabba = GabbaGMP::GabbaGMP.new("tracker123", request, cookies)
38
+ gabba.event("Cats", "Action")
39
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker123", cid: "1234", t: "event",
40
+ ec: "Cats", ea: "Action", dr: "http://www.otherexample.com/my-pages"}))
41
+ end
42
+
43
+ it "#initialize(ga_tracking_id, request, cookies, options = {})" do
44
+ gabba = GabbaGMP::GabbaGMP.new("tracker123", request, cookies, client_id_cookie_sym: :visitor_id)
45
+ gabba.event("Cats", "Action")
46
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker123", cid: "567", t: "event",
47
+ ec: "Cats", ea: "Action"}))
48
+ end
49
+ end
50
+
51
+ describe "#add_options" do
52
+ let(:request) {MockRequest.new}
53
+ let(:cookies) {MockCookies.new(utm_visitor_uuid: 1234)}
54
+ let(:gabbaGmp) { GabbaGMP::GabbaGMP.new("tracker", request, cookies)}
55
+ before do
56
+ stub_analytics("")
57
+ end
58
+ it "#add_options(options)" do
59
+ gabbaGmp.add_options(user_language: "en-gb")
60
+ gabbaGmp.event("Cats", "Action")
61
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
62
+ ec: "Cats", ea: "Action", ul: "en-gb"}))
63
+
64
+ gabbaGmp.add_options(dimension_1: "X", dimension_2: "Y", dimension_3: "Z")
65
+ gabbaGmp.event("Cats", "Action")
66
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
67
+ ec: "Cats", ea: "Action", ul: "en-gb", cd1: "X", cd2: "Y", cd3: "Z"}))
68
+
69
+ gabbaGmp.set_custom_var(3, "Other Dimensions", "Time")
70
+ gabbaGmp.event("Cats", "Action")
71
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
72
+ ec: "Cats", ea: "Action", ul: "en-gb", cd1: "X", cd2: "Y", cd3: "Time"}))
73
+ end
74
+ end
75
+ describe "#campaign=" do
76
+ let(:request) {MockRequest.new}
77
+ let(:cookies) {MockCookies.new(utm_visitor_uuid: 1234)}
78
+ let(:gabbaGmp) { GabbaGMP::GabbaGMP.new("tracker", request, cookies)}
79
+ before do
80
+ stub_analytics("")
81
+ end
82
+ it "#add_options(options)" do
83
+ campaign = GabbaGMP::GabbaGMP::Campaign.new()
84
+ campaign.name = "First Campaign"
85
+ campaign.source = "Bottle"
86
+ campaign.medium = "Spiritual"
87
+ campaign.keyword = "Ring"
88
+ campaign.content = "Campaigns"
89
+
90
+ gabbaGmp.campaign = campaign
91
+ gabbaGmp.event("Cats1", "Action")
92
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
93
+ ec: "Cats1", ea: "Action", cn: "First Campaign", cs: "Bottle", cm: "Spiritual", ck: "Ring", cc: "Campaigns"}))
94
+
95
+ gabbaGmp.campaign = nil
96
+ gabbaGmp.event("Cats2", "Action")
97
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
98
+ ec: "Cats2", ea: "Action", cn: "First Campaign", cs: "Bottle", cm: "Spiritual", ck: "Ring", cc: "Campaigns"}))
99
+
100
+ campaign = GabbaGMP::GabbaGMP::Campaign.new()
101
+ campaign.keyword = "Rings"
102
+
103
+ gabbaGmp.campaign = campaign
104
+ gabbaGmp.event("Cats", "Action")
105
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
106
+ ec: "Cats", ea: "Action", cn: "(direct)", cs: "(direct)", cm: "(none)", ck: "Rings"}))
107
+
108
+ campaign = GabbaGMP::GabbaGMP::Campaign.new()
109
+ campaign.content = "Contents"
110
+ campaign.source = "Confidential"
111
+
112
+ gabbaGmp.campaign = campaign
113
+ gabbaGmp.event("Cats", "Action")
114
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
115
+ ec: "Cats", ea: "Action", cn: "(direct)", cs: "Confidential", cm: "(none)", cc: "Contents"}))
116
+ end
117
+ end
118
+ describe "#campaign=" do
119
+ let(:request) {MockRequest.new}
120
+ let(:cookies) {MockCookies.new(utm_visitor_uuid: 1234)}
121
+ let(:gabbaGmp) { GabbaGMP::GabbaGMP.new("tracker", request, cookies)}
122
+ before do
123
+ stub_analytics("")
124
+ end
125
+ it "@logger" do
126
+ class LoggerClass
127
+ attr_accessor :info_message
128
+ def info(message)
129
+ @info_message = message
130
+ end
131
+ end
132
+ gabbaGmp.logger = LoggerClass.new
133
+ gabbaGmp.event("Cats1", "Action")
134
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", tid: "tracker", cid: "1234", t: "event",
135
+ ec: "Cats1", ea: "Action"}))
136
+ expect(gabbaGmp.logger.info_message).to eq("GABBA_GMP: request params: v=1&tid=tracker&dh=www.example.com&cid=1234&uip=1.2.3.4&ua=Chrome%2F1.0.0.0.0&ul=en-us&t=event&ec=Cats1&ea=Action")
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ require 'pry'
3
+
4
+ describe GabbaGMP::GabbaGMP::PageView do
5
+ describe "#page_view" do
6
+ let(:request) {MockRequest.new}
7
+ let(:cookies) {MockCookies.new(utm_visitor_uuid: 1234)}
8
+ let(:gabbaGmp) { GabbaGMP::GabbaGMP.new("tracker", request, cookies)}
9
+ before do
10
+ stub_analytics("")
11
+ end
12
+
13
+ #request, title = nil, options = {}
14
+
15
+ it "#page_view(request)" do
16
+ gabbaGmp.page_view(request)
17
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "", tid: "tracker", cid: "1234", t: "pageview", dp: ""}))
18
+
19
+ request.fullpath = "/amazing/things"
20
+ gabbaGmp.page_view(request)
21
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "", tid: "tracker", cid: "1234", t: "pageview", dp: "/amazing/things"}))
22
+ end
23
+
24
+ it "#page_view(request, title)" do
25
+ gabbaGmp.page_view(request, "Title")
26
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "Title", tid: "tracker", cid: "1234", t: "pageview", dp: ""}))
27
+ end
28
+
29
+ it "#page_view(request, title, options)" do
30
+ gabbaGmp.page_view(request, "Title", {document_path: "/Nowhere"})
31
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "Title", tid: "tracker", cid: "1234", t: "pageview", dp: "/Nowhere"}))
32
+ end
33
+
34
+ it "must not interfere with other calls if using options" do
35
+ gabbaGmp.page_view(request, "Title", {document_path: "/Nowhere"})
36
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "Title", tid: "tracker", cid: "1234", t: "pageview", dp: "/Nowhere"}))
37
+
38
+ gabbaGmp.page_view(request)
39
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "", tid: "tracker", cid: "1234", t: "pageview", dp: ""}))
40
+
41
+ gabbaGmp.page_view(request, "Title", {user_language: "en-au"})
42
+ expect_query(MockRequest::DEFAULT_PARAMS.merge({v: "1", dt: "Title", tid: "tracker", cid: "1234", t: "pageview", ul: "en-au", dp: ""}))
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ require 'gabba-gmp'
2
+ require 'rspec'
3
+
4
+ require 'webmock/rspec'
5
+ WebMock.disable_net_connect!
6
+
7
+ def stub_analytics(expected_params)
8
+ stub_request(
9
+ :get,
10
+ /www.google-analytics.com\/collect?#{expected_params}/
11
+ ).to_return(:status => 200, :body => '', :headers => {})
12
+ end
13
+
14
+ def expect_query(query_params)
15
+ expect(WebMock).to have_requested(:get, "www.google-analytics.com\/collect").with(:query => query_params)
16
+ end
17
+
18
+ class MockCookies
19
+ def initialize(cookieSet = {})
20
+ @cookieList = cookieSet
21
+ end
22
+
23
+ def [](key)
24
+ @cookieList[key]
25
+ end
26
+ def []=(key, value)
27
+ @cookieList[key] = value[:value]
28
+ end
29
+ end
30
+ class MockRequest
31
+ DEFAULT_PARAMS = {dh: "www.example.com",
32
+ uip: "1.2.3.4",
33
+ ua: "Chrome/1.0.0.0.0",
34
+ ul: "en-us",
35
+ }
36
+ attr_accessor :host, :remote_ip, :user_agent, :accept_language, :referrer, :fullpath, :protocol, :host_with_port
37
+
38
+ def initialize()
39
+ @host = DEFAULT_PARAMS[:dh]
40
+ @remote_ip = DEFAULT_PARAMS[:uip]
41
+ @user_agent = DEFAULT_PARAMS[:ua]
42
+ @accept_language = DEFAULT_PARAMS[:ul]
43
+ @referrer = DEFAULT_PARAMS[:dr]
44
+ @fullpath = DEFAULT_PARAMS[:dp]
45
+ @protocol = "http://"
46
+ @host_with_port = "www.example.com"
47
+ end
48
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gabba-gmp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian West
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-01 00:00:00.000000000 Z
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-http-persistent
@@ -88,6 +88,12 @@ files:
88
88
  - lib/gabba-gmp/page_view.rb
89
89
  - lib/gabba-gmp/parameter_map.rb
90
90
  - lib/gabba-gmp/version.rb
91
+ - spec/lib/campaign_spec.rb
92
+ - spec/lib/custom_vars_spec.rb
93
+ - spec/lib/event_spec.rb
94
+ - spec/lib/gabba_gmp_spec.rb
95
+ - spec/lib/page_view_spec.rb
96
+ - spec/spec_helper.rb
91
97
  homepage: https://github.com/fuzzyjulz/gabba-gmp
92
98
  licenses:
93
99
  - MIT