gaevents 0.1.1 → 1.04

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
- SHA1:
3
- metadata.gz: e523230f45496a7bd32a3c62c07462367e12233f
4
- data.tar.gz: e3df85876a2a176efeccd1786a664b9f917a7dd9
2
+ SHA256:
3
+ metadata.gz: 31e17368516bd792f7be1a798d4b467705b64f7414bcaf424714f7b406f66ef5
4
+ data.tar.gz: 061faad9641e410ec34a94ecbf8c2fdb25c34959f9dcd206d0e43a4f5ae1181e
5
5
  SHA512:
6
- metadata.gz: 9f42e64a7acf73c89c3e7cbbdd5cc338b0144a3607956a47052e0a69e481dafe6dac7a0f84e1ed99977c35bb8d9fa576fa9f5c6a08de542a89538109cf153527
7
- data.tar.gz: 7e0b058368e5dd442726f3cd94ac6deefc7bef8a8d7ea76603c3e0b38b736fb936a290a5f16d02c9e0c63fe657ef364f8683ae72ffdb1f2d2f375bc7ca96edef
6
+ metadata.gz: 29516d500929862fcb097832654f679441c37e4a15b05ab8d727d229657ecd44188952e065e94dbd32ab8b3886ced7c96d9c99cb5a8019bbfe3dd0ae627eaf9a
7
+ data.tar.gz: 07d1c6503ee27ed09e229edcb01fef99ab94b04445e0a7395cc05f423952ff4ce85cc8c40b4caa136ab4a1c1da5385307f23b152bf0259a5585b530b75853c57
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,4 @@
1
+ Style/Tab:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ Max: 100
@@ -0,0 +1 @@
1
+ language: ruby
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in gaevents.gemspec
4
4
  gemspec
5
+
6
+ group :development, :test do
7
+ gem 'rubocop', '~> 0.59.2', require: false
8
+ end
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
+ [![Gem Version](https://badge.fury.io/rb/gaevents.svg)](https://badge.fury.io/rb/gaevents)
2
+ [![Build Status](https://travis-ci.com/singhshivam/gaevents.svg?branch=master)](https://travis-ci.com/singhshivam/gaevents)
3
+
1
4
  # Gaevents
2
5
 
3
- This gem provides an integration for sending multiple events to Google Analytics. Events are sent in batches leveraging Measurement Protocol.
6
+ This gem allows you to update multiple background events in one go (via batches). Unlike other gems you do not need to load a js file to send events. Gaevents leverage GA's Measurement Protocol to send the events.
4
7
 
5
8
  ## Installation
6
9
 
@@ -20,16 +23,30 @@ Or install it yourself as:
20
23
 
21
24
  ## Usage
22
25
 
23
- ```
24
- # configure your application's API key
25
- GAEvents.api_key = "UA-XXXXX-Y"
26
+ Please refer [Measurement Protocol Parameter Reference](https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters) for the list of all parameters accepted by the Protocol.
27
+
28
+ The following parameters are required in each event:
29
+ `v`, `tid`, `cid` and `t`. This gem automatically injects `v`, make sure all events have `tid`, `cid` and `t` as parameters.
26
30
 
31
+ ```
27
32
  events = []
28
33
  10.times { |n|
29
- events << GAEvents::Event.new(GOOGLE_API_CLIENT_ID, "testcategory", "gaaction#{n}")
34
+ events << GAEvents::Event.new({tid: GATRACKINGID, cid: "ci#{n}", t: 'event', ec: "video#{n}", ea: "abc#{n}", uid: "user#{n}"})
30
35
  }
31
36
  GAEvents.track(events)
32
37
  ```
38
+
39
+ ## Migrating from 0.x to 1.x
40
+
41
+ In 0.x versions events were restricted to accepting only 5 parameters: `cid`, `ec`, `ea`, `el` and `ev` in the same order. Example:
42
+ ```
43
+ GAEvents::Event.new(GOOGLE_API_CLIENT_ID, "testcategory", "gaaction")
44
+ ```
45
+ 1.x now accepts a hash that can have any number of acceptable parameters. The above line of code then becomes:
46
+ ```
47
+ GAEvents::Event.new({tid: GATRACKINGID, cid: GOOGLE_API_CLIENT_ID, ec: "testcategory", ea: "gaaction"})
48
+ ```
49
+
33
50
  ## Development
34
51
 
35
52
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "gaevents"
3
+ require 'bundler/setup'
4
+ require 'gaevents'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "gaevents"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
@@ -1,27 +1,32 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('lib', __dir__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require 'gaevents/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "gaevents"
6
+ spec.name = 'gaevents'
8
7
  spec.version = Gaevents::VERSION
9
- spec.authors = ["singhshivam"]
10
- spec.email = ["shivam@blogvault.net"]
8
+ spec.authors = ['singhshivam']
9
+ spec.email = ['shivam@blogvault.net']
11
10
 
12
- spec.summary = %q{Sends events to google analytics in batches leveraging Measurement Protocol}
13
- spec.description = %q{Provides an integration for sending multiple events to Google Analytics}
14
- spec.homepage = "https://github.com/singhshivam/gaevents"
15
- spec.license = "MIT"
11
+ spec.description = 'Provides an integration for sending multiple events to Google Analytics'
12
+ spec.summary = 'This gem allows you to update multiple background events ' \
13
+ 'in one go (via batches). Unlike other gems ' \
14
+ 'you dont need to load a js file to send events. ' \
15
+ 'Gaevents leverage GA\'s Measurement Protocol.'
16
+
17
+ spec.homepage = 'https://github.com/singhshivam/gaevents'
18
+ spec.license = 'MIT'
16
19
 
17
20
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
21
  f.match(%r{^(test|spec|features)/})
19
22
  end
20
- spec.bindir = "exe"
23
+ spec.bindir = 'exe'
21
24
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
- spec.require_paths = ["lib"]
25
+ spec.require_paths = ['lib']
23
26
 
24
- spec.add_development_dependency "bundler", "~> 1.14"
25
- spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency 'bundler', '~> 1.14'
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency "rake", ">= 12.3.3"
30
+ spec.add_development_dependency 'rspec'
26
31
  spec.add_runtime_dependency 'rest-client', '~> 2.0', '>= 2.0.2'
27
32
  end
@@ -1,32 +1,34 @@
1
- require "gaevents/version"
2
- require "rest-client"
3
- require_relative "gaevents/event"
1
+ require 'gaevents/version'
2
+ require 'rest-client'
3
+ require_relative 'gaevents/event'
4
4
 
5
+ # collection of events
5
6
  class GAEvents
6
- BULK_URI = 'https://www.google-analytics.com/batch'.freeze
7
- COLLECT_URI = 'https://www.google-analytics.com/batch'.freeze
7
+ BULK_URI = 'https://www.google-analytics.com/batch'.freeze
8
+ COLLECT_URI = 'https://www.google-analytics.com/collect'.freeze
8
9
 
9
- class << self
10
- attr_accessor :api_key
10
+ class << self
11
+ attr_accessor :api_key
11
12
 
12
- def send_event(event)
13
- resource = RestClient::Resource.new COLLECT_URI
14
- resource.post(track_body([event]), {:content_type => 'text/plain'})
15
- end
13
+ def send_event(event)
14
+ resource = RestClient::Resource.new COLLECT_URI
15
+ resource.post(track_body([event]), content_type: 'text/plain')
16
+ end
16
17
 
17
- def track(*events)
18
- # as per GA: A maximum of 20 hits can be specified per request
19
- # https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch
20
- res = []
21
- events.each_slice(20) { |_events|
22
- resource = RestClient::Resource.new BULK_URI
23
- res << resource.post(track_body(_events.compact), {:content_type => 'text/plain'})
24
- }
25
- res
26
- end
18
+ # takes Array of Event as parameter
19
+ def track(events)
20
+ # as per GA: A maximum of 20 hits can be specified per request
21
+ # https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch
22
+ res = []
23
+ events.each_slice(20) do |events_slice|
24
+ resource = RestClient::Resource.new BULK_URI
25
+ res << resource.post(track_body(events_slice.compact), content_type: 'text/plain')
26
+ end
27
+ res
28
+ end
27
29
 
28
- def track_body(*events)
29
- events.flatten.map { |_event| _event.payload(api_key) }.join("\n")
30
- end
31
- end
30
+ def track_body(*events)
31
+ events.flatten.map(&:payload).join("\n")
32
+ end
33
+ end
32
34
  end
@@ -1,31 +1,26 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class GAEvents
2
- class Event
3
- attr_accessor :client, :category, :action, :label, :value
4
+ # Class to define analytical events
5
+ # expected attributes: tid, cid and t
6
+ class Event
7
+ attr_accessor :params
4
8
 
5
- def initialize(client, category, action, label = nil, value = nil)
6
- @client = client
7
- @category = category
8
- @action = action
9
- @label = label
10
- @value = value
11
- end
9
+ # Initialize Events by passing a hash.
10
+ # Keys could be any GA allowed parameter.
11
+ # Please refer Measurement Protocol Parameter Reference for available options:
12
+ # https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
12
13
 
13
- def params(tid)
14
- params = {
15
- "v" => 1,
16
- "tid" => tid,
17
- "cid" => @client,
18
- "t" => "event",
19
- "ec" => @category,
20
- "ea" => @action,
21
- "el" => @label,
22
- "ev" => @value
23
- }
24
- params.reject! { |k,v| !v }
25
- end
14
+ # As per Measurement Protocol, parameters: v, tid, cid and t should always be present.
15
+ # This gem automatically injects v. Ensure you always pass tid, cid and t while
16
+ # initializing events.
17
+ def initialize(hash = {})
18
+ @params = hash.select { |_k, v| v }
19
+ @params['v'] = 1
20
+ end
26
21
 
27
- def payload(tid)
28
- URI.encode_www_form params(tid)
29
- end
30
- end
22
+ def payload
23
+ URI.encode_www_form(@params)
24
+ end
25
+ end
31
26
  end
@@ -1,3 +1,3 @@
1
1
  module Gaevents
2
- VERSION = "0.1.1"
2
+ VERSION = '1.04'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gaevents
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '1.04'
5
5
  platform: ruby
6
6
  authors:
7
7
  - singhshivam
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-13 00:00:00.000000000 Z
11
+ date: 2020-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,20 +24,48 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '10.0'
47
+ version: 12.3.3
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 12.3.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
39
67
  - !ruby/object:Gem::Version
40
- version: '10.0'
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rest-client
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +94,9 @@ extensions: []
66
94
  extra_rdoc_files: []
67
95
  files:
68
96
  - ".gitignore"
97
+ - ".rspec"
98
+ - ".rubocop.yml"
99
+ - ".travis.yml"
69
100
  - CODE_OF_CONDUCT.md
70
101
  - Gemfile
71
102
  - LICENSE.txt
@@ -96,9 +127,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
127
  - !ruby/object:Gem::Version
97
128
  version: '0'
98
129
  requirements: []
99
- rubyforge_project:
100
- rubygems_version: 2.5.2
130
+ rubygems_version: 3.0.3
101
131
  signing_key:
102
132
  specification_version: 4
103
- summary: Sends events to google analytics in batches leveraging Measurement Protocol
133
+ summary: This gem allows you to update multiple background events in one go (via batches).
134
+ Unlike other gems you dont need to load a js file to send events. Gaevents leverage
135
+ GA's Measurement Protocol.
104
136
  test_files: []