woopra_track 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b7223a15d2694d6f1fff24368c65052dd3074bd
4
+ data.tar.gz: 5d4e05376eb54ff80f73adc72fbdb0438a76771e
5
+ SHA512:
6
+ metadata.gz: ef3905755fbae9609a907ac9b828ba3706c6006a0e8cb9c2ea20845457691ae926ab43bfcdc8abe2a826bde2e897dfa7e735f0c8e3d6d7573288b1db3870893a
7
+ data.tar.gz: 7b983f399f2855fb2e68be53b4706d37d291e42faf8f9bf2ad7fc84091cce43c6621a1ee5785bba633f0b026c645acb2b82e3446f8724b80f1b85e5ec3e897f4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in woopra_track.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Jonian Guveli
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # WoopraTrack
2
+
3
+ Tracking library for woopra.com. Woopra analytics client-side and server-side tracking helper.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'woopra_track'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install woopra_track
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hardpixel/woopra-track.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,7 @@
1
+ module WoopraTrack
2
+ module Helper
3
+ def woopra_javascript_tag
4
+ @woopra.try :javascript_tag
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,170 @@
1
+ require 'uri'
2
+ require 'typhoeus'
3
+ require 'logger'
4
+
5
+ module WoopraTrack
6
+ class Tracker
7
+ @@application_id = 'rails'
8
+ @@default_config = {
9
+ domain: nil,
10
+ cookie_name: 'wooTracker',
11
+ cookie_domain: nil,
12
+ cookie_path: '/',
13
+ ping: true,
14
+ ping_interval: 12000,
15
+ idle_timeout: 300000,
16
+ download_tracking: true,
17
+ outgoing_tracking: true,
18
+ download_pause: 200,
19
+ outgoing_pause: 400,
20
+ ignore_query_url: true,
21
+ hide_campaign: false,
22
+ ip_address: nil,
23
+ cookie_value: nil,
24
+ }
25
+
26
+ def initialize(request)
27
+ @request = request
28
+ @current_config = @@default_config
29
+ @custom_config = { app: @@application_id }
30
+ @user = {}
31
+ @events = []
32
+ @user_up_to_date = true
33
+ @has_pushed = false
34
+
35
+ @current_config[:domain] = @request.host
36
+ @current_config[:cookie_domain] = @request.host
37
+ @current_config[:ip_address] = get_client_ip
38
+ @current_config[:cookie_value] = @request.cookies[@current_config[:cookie_name]] || random_cookie
39
+ end
40
+
41
+ def enable_logging
42
+ @logger = Logger.new(STDOUT)
43
+ end
44
+
45
+ def config(data)
46
+ data = Hash(data).select { |k, _v| k.in? @@default_config.keys }
47
+ data = data.except(:ip_address, :cookie_value)
48
+
49
+ @custom_config.merge!(data)
50
+ end
51
+
52
+ def identify(user)
53
+ @user = user
54
+ @user_up_to_date = false
55
+ end
56
+
57
+ def track(*args)
58
+ event_name = nil
59
+ event_data = nil
60
+ back_end = false
61
+
62
+ args.each do |param|
63
+ case param
64
+ when String
65
+ event_name = param
66
+ when Hash
67
+ event_data = param
68
+ when TrueClass
69
+ back_end = param
70
+ end
71
+ end
72
+
73
+ if back_end
74
+ http_request([event_name, event_data])
75
+ else
76
+ @events << [event_name, event_data]
77
+ end
78
+ end
79
+
80
+ def push(back_end=false)
81
+ if not @user_up_to_date
82
+ if back_end
83
+ http_request()
84
+ else
85
+ @has_pushed = true
86
+ end
87
+ end
88
+ end
89
+
90
+ def javascript_tag
91
+ code = ['(function(){var t,i,e,n=window,o=document,a=arguments,s="script",r=["config","track","trackForm","trackClick","identify","visit","push","call"],c=function(){var t,i=this;for(i._e=[],t=0;r.length>t;t++)(function(t){i[t]=function(){return i._e.push([t].concat(Array.prototype.slice.call(arguments,0))),i}})(r[t])};for(n._w=n._w||{},t=0;a.length>t;t++)n._w[a[t]]=n[a[t]]=n[a[t]]||new c;i=o.createElement(s),i.async=1,i.src="//static.woopra.com/js/w.js",e=o.getElementsByTagName(s)[0],e.parentNode.insertBefore(i,e)})("woopra");']
92
+
93
+ code << "woopra.config(#{@custom_config.to_json});" if @custom_config.length != 0
94
+ code << "woopra.identify(#{@user.to_json});" if not @user_up_to_date
95
+
96
+ @events.each do |event|
97
+ if event.first.nil?
98
+ code << "woopra.track();"
99
+ else
100
+ code << "woopra.track('#{event.first}', #{event.second.to_json});"
101
+ end
102
+ end
103
+
104
+ code << "woopra.push();" if @has_pushed
105
+
106
+ "<script>\n#{code.join("\n")}\n</script>".html_safe
107
+ end
108
+
109
+ def set_cookie(cookies)
110
+ cookies[@current_config[:cookie_name]] = @current_config[:cookie_value]
111
+ end
112
+
113
+ private
114
+
115
+ def http_request(event=nil)
116
+ request_url = 'https://www.woopra.com'
117
+ get_params = {
118
+ host: @current_config[:domain],
119
+ cookie: @current_config[:cookie_value],
120
+ ip: @current_config[:ip_address],
121
+ timeout: @current_config[:idle_timeout]
122
+ }
123
+
124
+ user_params = Hash[@user.map { |k, v| [:"cv_#{k}", "#{v}"] }]
125
+ get_params = get_params.merge(user_params)
126
+
127
+ if event.nil?
128
+ request_url = URI.join(request_url, 'track/identify')
129
+ else
130
+ request_url = URI.join(request_url, 'track/ce')
131
+
132
+ if event.first.nil?
133
+ get_params = get_params.merge(event: 'pv', ce_url: @request.url)
134
+ else
135
+ event_data = Hash[Array(event.second).map { |k, v| [:"ce_#{k}", "#{v}"] }]
136
+ get_params = get_params.merge(event: event.first).merge(event_data)
137
+ end
138
+ end
139
+
140
+ request_url.query = URI.encode_www_form(get_params.merge(ce_app: @@application_id))
141
+ request_headers = { 'User-Agent' => @request.env['HTTP_USER_AGENT'] }
142
+ request_response = Typhoeus.get(request_url, headers: request_headers)
143
+
144
+ if @logger
145
+ if request_response.success?
146
+ @logger.info("Woopra") { "Success: #{request_url}" }
147
+ elsif request_response.timed_out?
148
+ @logger.warn("Woopra") { "Timeout: #{request_url}" }
149
+ elsif request_response.code == 0
150
+ @logger.error("Woopra") { "#{request_response.return_message}, #{request_url}" }
151
+ else
152
+ @logger.error("Woopra") { "WOOPRA Failed: #{request_response.code.to_s}, #{request_url}" }
153
+ end
154
+ end
155
+ end
156
+
157
+ def random_cookie
158
+ o = [('0'..'9'), ('A'..'Z')].map { |i| i.to_a }.flatten
159
+ (0..12).map { o[rand(o.length)] }.join
160
+ end
161
+
162
+ def get_client_ip
163
+ if not @request.env['HTTP_X_FORWARDED_FOR'].nil?
164
+ @request.env['HTTP_X_FORWARDED_FOR'].split(',').first.strip
165
+ else
166
+ @request.remote_ip
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,3 @@
1
+ module WoopraTrack
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'woopra_track/version'
2
+ require 'woopra_track/helper'
3
+
4
+ module WoopraTrack
5
+ autoload :Tracker, 'woopra_track/tracker'
6
+
7
+ class << self
8
+ def included(model_class)
9
+ model_class.extend self
10
+ end
11
+ end
12
+
13
+ def woopra(request, config=nil, cookies=nil)
14
+ @woopra = Tracker.new request
15
+
16
+ @woopra.config(config) unless config.nil?
17
+ @woopra.set_cookie(cookies) unless cookies.nil?
18
+ end
19
+ end
20
+
21
+ if defined? ActionView::Base
22
+ ActionView::Base.send :include, WoopraTrack::Helper
23
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: woopra_track
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonian Guveli
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: typhoeus
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description: Woopra analytics client-side and server-side tracking helper.
70
+ email:
71
+ - jonian@hardpixel.eu
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - Gemfile
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - lib/woopra_track.rb
81
+ - lib/woopra_track/helper.rb
82
+ - lib/woopra_track/tracker.rb
83
+ - lib/woopra_track/version.rb
84
+ homepage: https://github.com/hardpixel/woopra-track
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.6.11
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Tracking library for woopra.com
108
+ test_files: []