gosquared-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/Gemfile +9 -0
- data/LICENSE +21 -0
- data/README.md +100 -0
- data/lib/configuration.rb +60 -0
- data/lib/gosquared-rails.rb +17 -0
- data/lib/rails/generators/gosquared_rails/config/config_generator.rb +16 -0
- data/lib/rails/generators/gosquared_rails/config/templates/gosquared_rails.rb.erb +3 -0
- data/lib/tracker_inject/injector.rb +85 -0
- data/lib/tracker_inject/property_config.rb +47 -0
- data/lib/tracker_inject/railtie.rb +8 -0
- data/spec/configuration_spec.rb +104 -0
- data/spec/gosquared_generator_spec.rb +19 -0
- data/spec/gosquared_spec.rb +40 -0
- data/spec/helpers/anonymous_controller_helper_spec.rb +35 -0
- data/spec/internal/app/config/initializers/gosquared_rails.rb +2 -0
- data/spec/internal/app/generators/config_generator.rb +16 -0
- data/spec/internal/app/generators/templates/gosquared_rails.rb.erb +2 -0
- data/spec/internal/app/models/user.rb +3 -0
- data/spec/internal/log/test.log +2574 -0
- data/spec/property_config_spec.rb +34 -0
- data/spec/spec_helper.rb +117 -0
- data/spec/tracker_intject_spec.rb +27 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 29ecc8597ac248b671b4b05b4fb9c22d834c991a
|
4
|
+
data.tar.gz: d1265f50e4ac015bd70a0a822b00bfb596ddb433
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0a29e5ad98dca630c1ea21b0d66312b35e573c74b2b89706242d5c089873326674a9d01beaf79c34002586e2480a1470aeb06f5cc42bb2d990c55df4bce3e7aa
|
7
|
+
data.tar.gz: 2b3e393ef71184910d13b6ccf9e22ac1e0c5e3937421c7eb77e3f5b1da039d3e3d3914c901cf15a2868560c733a2e792091878e67bab962256624bfe0628e8cb
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015-2016 Go Squared Ltd.
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
## GoSquared Rails Gem
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/gosquared/gosquared-rails.svg?branch=master)](https://travis-ci.org/gosquared/gosquared-rails)
|
4
|
+
|
5
|
+
The GoSquared Rails Gem will allow you to quickly and easily install GoSquared Chat, Anayltics and People in your Rails app.
|
6
|
+
|
7
|
+
If you are looking to interact with our Tracking or Reporting API's, you can do so by using our [Ruby Client]
|
8
|
+
(https://github.com/gosquared/ruby-client)
|
9
|
+
|
10
|
+
Note, this gem will inject the GoSquared JavaScript snippet as well as GoSquared Identify method in your front end. If you currently have GoSquared JavaScript snippets in place, this can create a conflict.
|
11
|
+
|
12
|
+
**This is an early beta, please open an issue if you find anything not working, or to leave feedback for improvement. You can also get in touch directly: russell@gosquared.com**
|
13
|
+
|
14
|
+
### Install of Analytics and Chat
|
15
|
+
|
16
|
+
With the GoSquared Rails gem you can quickly install GoSquared's tracking code on your app. GoSquared uses a javascript code snippet to track pageviews and optionally load the GoSquared Live Chat widget.
|
17
|
+
|
18
|
+
If you don't have a GoSquared account and you can sign up for one [here](https://www.gosquared.com/join/) and get your project token. Then you can quickly add the code to all of your Rails’ views by running this:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
rails generate gosquared_rails:config 'your_project_token'
|
22
|
+
```
|
23
|
+
|
24
|
+
This will insert a `<script>` tag automatically before the closing `</head>` tag on each view rendered.
|
25
|
+
|
26
|
+
After generating your config file, if there are any controllers you would prefer not to have the tracking code automatically inserted in, you'll just need to add the following to that specific controller:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
skip_after_action :add_gosquared_script
|
30
|
+
```
|
31
|
+
|
32
|
+
### Configuration
|
33
|
+
|
34
|
+
To configure GoSquared Analytics tracking, you can do so in ```conifg/initializers/gosquared_rails.rb```
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
config.track_local = true
|
38
|
+
```
|
39
|
+
Whether to track data and load Live Chat on local pages/sites (using the file:// protocol, or on localhost). This helps prevent local development from polluting your stats.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
config.anonymize_ip = true
|
43
|
+
```
|
44
|
+
Setting this value to true will prevent the visitors' IP address from being tracked.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
config.cookie_domain = '.your-domain.com'
|
48
|
+
```
|
49
|
+
By default, cookie_domain will be the current domain (including subdomain). This means that IDs will not be shared across subdomains. Set this to .your-domain.com to enable accurate cross-subdomain tracking.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
config.use_cookies = false
|
53
|
+
```
|
54
|
+
Set to false to disable usage of cookies in the tracker (for example, if your site is subject to strict cookie legislation). This will mean that certain parts of GoSquared will be inaccurate.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
config.referrer = 'some_other_domain'
|
58
|
+
```
|
59
|
+
Use this option to override the value of the page's referring URL. This is useful if you know the source of the pageview via another tracking system.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
config.track_hash = true
|
63
|
+
```
|
64
|
+
Whether to track hashes in the page URL, for example /home#my-hash will, by default, be tracked as /home.
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
config.track_params = false
|
68
|
+
```
|
69
|
+
Whether to track URL querystring parameters, for example /home?my=query&string=true will be tracked as /home if this is set to false.
|
70
|
+
|
71
|
+
For more details on configuration, you can view the docs here https://www.gosquared.com/docs/api/javascript-tracking-code/configuration#
|
72
|
+
|
73
|
+
### Installation of People
|
74
|
+
|
75
|
+
If you have a user system and would like to track down to an individual user level, you can do so by including the GoSquared Identify Method in your ApplicationController (or any indvidual controller you would like to track.)
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
append_after_filter {|controller| controller.add_gosquared_identify_method your_current_user_object }
|
79
|
+
```
|
80
|
+
|
81
|
+
Then just define the following method in the respective controller and pass in a hash of any properties you'd like to track
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
def gosquared_user_properties
|
85
|
+
GosquaredRails.configure.custom_properties = {id: current_user_object.id, email: current_user_object.email,
|
86
|
+
phone: current_user_object.phone_number,
|
87
|
+
monthly_mrr: current_user_object.mrr,
|
88
|
+
role_of_user: current_user_object.role
|
89
|
+
any_other_property: 'you'd like to track'}
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
This will insert a `<script>` tag automatically before the closing `</body>` tag on each view rendered.
|
94
|
+
|
95
|
+
|
96
|
+
#Tests
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
rspec
|
100
|
+
```
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module GosquaredRails
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :site_token, :track_local, :anonymize_ip, :cookie_domain,
|
4
|
+
:use_cookies, :referrer, :track_hash, :track_params, :identified_user, :custom_properties
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@site_token = nil
|
8
|
+
@track_local = nil
|
9
|
+
@anonymize_ip = nil
|
10
|
+
@cookie_domain = nil
|
11
|
+
@use_cookies = nil
|
12
|
+
@referrer = nil
|
13
|
+
@track_hash = nil
|
14
|
+
@track_params = nil
|
15
|
+
@identified_user = nil
|
16
|
+
@custom_properties = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
def config_options
|
20
|
+
@configurations = [set_track_local, set_anonymize_ip,
|
21
|
+
set_cookie_domain, set_use_cookies, set_referrer, set_track_hash, set_track_params]
|
22
|
+
@display_config = []
|
23
|
+
@configurations.each do |config|
|
24
|
+
@display_config << config if config
|
25
|
+
end
|
26
|
+
@display_config.join(" ")
|
27
|
+
end
|
28
|
+
|
29
|
+
def set_track_local
|
30
|
+
"_gs('set', 'trackLocal', true);" if track_local == true
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_anonymize_ip
|
34
|
+
"_gs('set', 'anonymizeIP', true);" if @anonymize_ip == true
|
35
|
+
end
|
36
|
+
|
37
|
+
def set_cookie_domain
|
38
|
+
"_gs('set', 'cookieDomain', '#{@cookie_domain}');" if @cookie_domain
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_use_cookies
|
42
|
+
"_gs('set', 'useCookies', false);" if @use_cookies == false
|
43
|
+
end
|
44
|
+
|
45
|
+
def set_referrer
|
46
|
+
"_gs('set', 'referrer', '#{@referrer}');" if @referrer
|
47
|
+
end
|
48
|
+
|
49
|
+
def set_track_hash
|
50
|
+
"_gs('set', 'trackHash', true);" if @track_hash == true
|
51
|
+
end
|
52
|
+
|
53
|
+
def set_track_params
|
54
|
+
"_gs('set', 'trackParams', false);" if @track_params == false
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'tracker_inject/railtie' if defined? (Rails)
|
2
|
+
require_relative "configuration"
|
3
|
+
|
4
|
+
module GosquaredRails
|
5
|
+
class << self
|
6
|
+
attr_accessor :configuration
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.configuration
|
10
|
+
@configuration ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.configure
|
14
|
+
block_given? ? yield(configuration) : configuration
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators/base'
|
2
|
+
|
3
|
+
module GosquaredRails
|
4
|
+
module Generators
|
5
|
+
class ConfigGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("../templates", __FILE__)
|
7
|
+
|
8
|
+
argument :site_token
|
9
|
+
|
10
|
+
def copy_initializer_file
|
11
|
+
@site_token = site_token
|
12
|
+
template("gosquared_rails.rb.erb", File.join("config/initializers/gosquared_rails.rb"))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require_relative "property_config.rb"
|
2
|
+
|
3
|
+
class Injector
|
4
|
+
module Filter
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
included do
|
7
|
+
append_after_filter :add_gosquared_script, :if => :html_response?
|
8
|
+
|
9
|
+
CLOSING_HEAD_TAG = %r{</head>}
|
10
|
+
CLOSING_BODY_TAG = %r{</body>}
|
11
|
+
|
12
|
+
def add_gosquared_script
|
13
|
+
response.body = response.body.gsub(CLOSING_HEAD_TAG, "<script>
|
14
|
+
|
15
|
+
(function() {
|
16
|
+
if (window._gs) return;
|
17
|
+
!function(g,s,q,r,d){r=g[r]=g[r]||function(){(r.q=r.q||[]).push(
|
18
|
+
arguments)};d=s.createElement(q);q=s.getElementsByTagName(q)[0];
|
19
|
+
d.src='//d1l6p2sc9645hc.cloudfront.net/tracker.js';q.parentNode.
|
20
|
+
insertBefore(d,q)}(window,document,'script','_gs');
|
21
|
+
_gs('#{GosquaredRails.configure.site_token}', false);
|
22
|
+
#{GosquaredRails.configure.config_options}
|
23
|
+
|
24
|
+
|
25
|
+
function track() {
|
26
|
+
_gs('track');
|
27
|
+
}
|
28
|
+
$(document).on('page:load', track);
|
29
|
+
$(document).on('turbolinks:load', track);
|
30
|
+
|
31
|
+
$(document)
|
32
|
+
.on('turbolinks:before-render', function(event){
|
33
|
+
var chat = $('[id^=\"gs_\"]');
|
34
|
+
if (chat.length) {
|
35
|
+
chat
|
36
|
+
.detach()
|
37
|
+
.appendTo(event.originalEvent.data.newBody);
|
38
|
+
}
|
39
|
+
})
|
40
|
+
.on('turbolinks:render', function() {
|
41
|
+
try {
|
42
|
+
window.dispatchEvent(new Event('resize'));
|
43
|
+
} catch (e) {}
|
44
|
+
})
|
45
|
+
})();
|
46
|
+
|
47
|
+
</script>" + "\n </head>"
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
def add_gosquared_identify_method(current_user)
|
52
|
+
if current_user
|
53
|
+
begin
|
54
|
+
gosquared_user_properties
|
55
|
+
rescue NameError
|
56
|
+
STDERR.puts "ERROR: The #gosquared_user_properties method must be added to the respective controller, please see docs"
|
57
|
+
end
|
58
|
+
populate_script
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def populate_script(property_config=PropertyConfig.new)
|
65
|
+
unless GosquaredRails.configure.custom_properties.nil?
|
66
|
+
property_config.sort_property_fields(GosquaredRails.configure.custom_properties)
|
67
|
+
response.body = response.body.gsub(CLOSING_BODY_TAG, "<script>
|
68
|
+
_gs('identify',
|
69
|
+
#{property_config.gosquared_standard_properties}
|
70
|
+
#{property_config.gosquared_custom_properties}
|
71
|
+
});
|
72
|
+
</script>" + "\n </body>"
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def html_response?
|
78
|
+
response.content_type == "text/html"
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
class PropertyConfig
|
3
|
+
|
4
|
+
def sort_property_fields(hash)
|
5
|
+
validate_properties(hash)
|
6
|
+
property_fields = ['id', 'email', 'name', 'first_name', 'last_name',
|
7
|
+
'username', 'phone', 'created_at']
|
8
|
+
|
9
|
+
@standard_properties_hash = {}
|
10
|
+
@custom_properties_hash = {}
|
11
|
+
hash.each do | key, value |
|
12
|
+
property_fields.each do | property |
|
13
|
+
if key.to_s === property
|
14
|
+
@standard_properties_hash[key] = value
|
15
|
+
hash.except!(key)
|
16
|
+
end
|
17
|
+
@custom_properties_hash = hash
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def gosquared_custom_properties
|
23
|
+
@custom_properties = "custom: { \n "
|
24
|
+
@custom_properties_hash.each do |key, value|
|
25
|
+
@custom_properties << "#{key}: '#{value}',\n "
|
26
|
+
end
|
27
|
+
@custom_properties << '}'
|
28
|
+
end
|
29
|
+
|
30
|
+
def gosquared_standard_properties
|
31
|
+
@standard_properties = " { \n "
|
32
|
+
@standard_properties_hash.each do |key, value|
|
33
|
+
@standard_properties << "#{key}: '#{value}',\n "
|
34
|
+
end
|
35
|
+
@standard_properties << '}' if @custom_properties == "custom: { \n }"
|
36
|
+
@standard_properties
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def validate_properties(property_hash)
|
42
|
+
unless property_hash.key?(:id) || property_hash.key(:email)
|
43
|
+
STDERR.puts "ERROR: GoSquared User properties must include id or email address"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe GosquaredRails::Configuration do
|
4
|
+
|
5
|
+
subject(:gs) { described_class.new() }
|
6
|
+
|
7
|
+
describe '#site_token' do
|
8
|
+
it 'responds to #site_token' do
|
9
|
+
expect(gs).to respond_to(:site_token)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#set_local_configuration' do
|
14
|
+
it 'returns _gs track local when track_local is true' do
|
15
|
+
gs.track_local = true
|
16
|
+
expect(gs.set_track_local).to eq("_gs('set', 'trackLocal', true);")
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'returns nil when not true' do
|
20
|
+
expect(gs.set_track_local).to eq nil
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#set_anonymize_ip' do
|
26
|
+
it 'returns _gs anonymize_ip true' do
|
27
|
+
gs.anonymize_ip = true
|
28
|
+
expect(gs.set_anonymize_ip).to eq("_gs('set', 'anonymizeIP', true);")
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'returns nil when anonymize_ip is not true' do
|
32
|
+
expect(gs.set_anonymize_ip).to eq nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#set_use_cookies' do
|
37
|
+
it 'returns _gs useCookies true when use_cookies is true' do
|
38
|
+
gs.use_cookies = false
|
39
|
+
expect(gs.set_use_cookies).to eq("_gs('set', 'useCookies', false);")
|
40
|
+
end
|
41
|
+
it 'returns nil when use_cookies is not true' do
|
42
|
+
expect(gs.set_use_cookies).to eq nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#set_cookie_domain' do
|
47
|
+
it 'returns _gs cookieDomain undefined when cookie_domain is true' do
|
48
|
+
gs.cookie_domain = '.your_domain.com'
|
49
|
+
expect(gs.set_cookie_domain).to eq("_gs('set', 'cookieDomain', '.your_domain.com');")
|
50
|
+
end
|
51
|
+
it 'returns nil when cookie_domain is not true' do
|
52
|
+
expect(gs.set_cookie_domain).to eq nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
describe '#set_referrer' do
|
58
|
+
it 'returns _gs referrer document.referrer when referrer is true' do
|
59
|
+
gs.referrer = 'some other url'
|
60
|
+
expect(gs.set_referrer).to eq("_gs('set', 'referrer', 'some other url');")
|
61
|
+
end
|
62
|
+
it 'returns nil when set_referrer is not true' do
|
63
|
+
expect(gs.set_referrer).to eq nil
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#set_track_hash' do
|
68
|
+
it 'returns _gs trackHash false when track_hash is true' do
|
69
|
+
gs.track_hash = true
|
70
|
+
expect(gs.set_track_hash).to eq("_gs('set', 'trackHash', true);")
|
71
|
+
end
|
72
|
+
it 'returns nil when track_hash is not true' do
|
73
|
+
expect(gs.set_track_hash).to eq nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#set_track_params' do
|
78
|
+
it 'returns _gs trackParams true when track_params is true' do
|
79
|
+
gs.track_params = false
|
80
|
+
expect(gs.set_track_params).to eq("_gs('set', 'trackParams', false);")
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#config_options' do
|
85
|
+
it 'responds to #config_options' do
|
86
|
+
expect(gs).to respond_to(:config_options)
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'returns config option if value associated' do
|
90
|
+
expect(gs.config_options).to eq ""
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'returns multiple config options if value associated' do
|
94
|
+
gs.track_hash = true
|
95
|
+
gs.track_params = false
|
96
|
+
expect(gs.config_options).to eq "_gs('set', 'trackHash', true); _gs('set', 'trackParams', false);"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
end
|
104
|
+
|