isc_analytics 0.5.4 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  A simple client-side & server-side analytics library for Rails. It currently supports only KISSmetrics for more advanced
3
3
  analytics features (trackEvent / setProperties) and Google Analytics just for tracking pageviews.
4
4
 
5
- ## Installation
5
+ ## Installation
6
6
 
7
7
  Just add the `isc_analytics` gem into your Gemfile
8
8
 
@@ -18,20 +18,26 @@ ISC Analytics currently supports the following configuration options:
18
18
 
19
19
 
20
20
  ```ruby
21
- IscAnalytics.config.accounts = ANALYTIC_ACCOUNTS # an accounts object (preferably using ConfigReader)
22
- IscAnalytics.config.namespace = 'App' # an alias which will gain all the analytics behivour in the clientside.
21
+ IscAnalytics.config.providers = ANALYTIC_PROVIDERS # a providers object (preferably using ConfigReader)
22
+ IscAnalytics.config.namespace = 'App' # an alias which will gain all the analytics behaviour in the clientside.
23
23
  ```
24
24
 
25
- We highly recommend you to use our other gem [ConfigReader](https://github.com/TheGiftsProject/configreader) to load the analytics_accounts data from a YML.
25
+ We highly recommend you to use our other gem
26
+ [ConfigReader](https://github.com/TheGiftsProject/configreader) to load the analytics_providers data from YML.
26
27
 
27
- The accounts hash / EnvConfigReader should contain the following sub keys:
28
+ The providers hash / EnvConfigReader should contain the following sub keys:
28
29
 
29
30
  ```yml
30
- kissmetrics_key: "KEY"
31
- google_analytics_key: "KEY"
32
- ipinfodb_key: "KEY"
33
- optimizely_key: "KEY"
34
- ```
31
+ kissmetrics:
32
+ key: "KEY"
33
+ dryrun: false # see http://support.kissmetrics.com/apis/ruby for more details
34
+ google_analytics:
35
+ key: "KEY"
36
+ ipinfodb:
37
+ key: "KEY"
38
+ optimizely:
39
+ key: "KEY"
40
+ ```
35
41
 
36
42
  Currently only the KISSMetrics and Google Analytics keys are mandatory although the gem isn't fully tested without the ipinfodb and optimizely options.
37
43
 
@@ -84,11 +90,25 @@ For example if I set my namespace as `App` then I'll be able to access the track
84
90
 
85
91
  ## Server Side
86
92
 
87
- All the **client side event tracking is available from the server side** as well. Once you've included isc_analytics you can access the `analytics` object which will have methods identical to the client-side analytics methods.
93
+ All the **client side event tracking is available from the server side** as well. Once you've included isc_analytics you can access the `analytics` object which will have methods identical to the client-side analytics methods.
88
94
  For example you can call `analytics.trackEvent('user-visit')` and it will be persisted in the current user session and flushed into the user's browser next time he visits a page (via add_analytics). This mechanism is similar in behaviour to Rails' flash object.
89
-
95
+
96
+ There is also support for purely server-side analytics (currently only for KISSMetrics) that send the data directly to KM, without going through the user's browser.
97
+ It is accessible through `IscAnalytics.server` with the following interface:
98
+ ```ruby
99
+ IscAnalytics.server.track_event(identity, name, properties={})
100
+ IscAnalytics.server.set_properties(identity, properties)
101
+ IscAnalytics.server.alias(identity, new_identity)
102
+ ```
103
+
90
104
  ### Opt out
91
105
 
92
- One very cool feature **isc_analytics** has is a built in opt_out feature to be used when testing the system or when doing admin actions.
93
- To enter opt_out for the current browsing session just call `analytics.opt_out!` from the controller.
106
+ One very cool feature **isc_analytics** has is a built in opt_out feature to be used when testing the system or when doing admin actions.
107
+ To enter opt_out for the current browsing session just call `analytics.opt_out!` from the controller.
94
108
  Once opt-ted out, a dummy implementation of the analytics js will be sent to the client meaning that no calls will actually be sent to the analytics service.
109
+
110
+ ## Notes
111
+ ### KISSMetrics URL api
112
+ After including IscAnalytics::ControllerSupport into your controller, you can override the [KISSMetrics URL api](http://support.kissmetrics.com/apis/url) by adding `before_filter keep_km_url_event` to your controller.
113
+ This records the url events manually as soon as the request comes in and allows you to use `redirect_to` without worrying about losing analytics data.
114
+ If you intend to use this, you should disable the URL api in your kissmetrics settings in order to avoid duplicate events.
data/lib/isc_analytics.rb CHANGED
@@ -4,6 +4,7 @@ require "#{File.dirname(__FILE__)}/isc_analytics/client_api"
4
4
  require "#{File.dirname(__FILE__)}/isc_analytics/exceptions"
5
5
  require "#{File.dirname(__FILE__)}/isc_analytics/bootstrap"
6
6
  require "#{File.dirname(__FILE__)}/isc_analytics/controller_support"
7
+ require "#{File.dirname(__FILE__)}/isc_analytics/server"
7
8
 
8
9
  module IscAnalytics
9
10
 
@@ -13,5 +14,9 @@ module IscAnalytics
13
14
  @configuration ||= IscAnalytics::Config.default
14
15
  end
15
16
 
17
+ def self.server
18
+ @server ||= IscAnalytics::Server.new(config.providers)
19
+ end
20
+
16
21
  end
17
22
 
@@ -6,7 +6,7 @@ module IscAnalytics
6
6
  include KISSMetricsClientAPI
7
7
 
8
8
  def initialize(options = {})
9
- validate_accounts_config
9
+ validate_providers_config
10
10
  @session = options[:session] || {}
11
11
  @opt_out = false
12
12
  end
@@ -23,35 +23,44 @@ module IscAnalytics
23
23
  Tags.scripts(analytics_scripts)
24
24
  end
25
25
 
26
+ def provider_script_tags
27
+ Tags.scripts(provider_scripts)
28
+ end
29
+
30
+ def queued_events
31
+ return nil if opt_out?
32
+ generate_queue_js
33
+ end
34
+
26
35
  private
27
36
 
28
37
  def analytics_scripts
29
38
  scripts = []
30
- scripts.concat Services.scripts(config.accounts) unless opt_out?
39
+ scripts.concat provider_scripts
31
40
  scripts << isc_analytics_tag
32
41
  scripts << extend_analytics(config.namespace)
33
- scripts << queued_events unless opt_out?
34
- scripts
42
+ scripts << Tags.script_tag(queued_events) unless queued_events.blank?
43
+ scripts.compact
44
+ end
45
+
46
+ def provider_scripts
47
+ opt_out? ? [] : Services.scripts(config.providers)
35
48
  end
36
49
 
37
50
  def config
38
51
  IscAnalytics.config
39
52
  end
40
53
 
41
- def validate_accounts_config
42
- raise IscAnalytics::NoConfigSpecified.new('You have specified a nil config, you must specify an EnvConfigReader of your Analytics Accounts keys') if config.accounts.nil?
43
-
44
- if config.accounts.kissmetrics_key.nil?
45
- raise IscAnalytics::MissingConfigParams.new('KISSMetrics configuration key isn\'t specified in your config.')
46
- elsif config.accounts.google_analytics_key.nil?
47
- raise IscAnalytics::MissingConfigParams.new('Google Analytics configuration key isn\'t specified in your config.')
54
+ def validate_providers_config
55
+ if config.providers.nil?
56
+ raise IscAnalytics::NoConfigSpecified.new('You have specified a nil config, you must specify an EnvConfigReader of your Analytics providers keys')
48
57
  end
49
- end
50
58
 
51
- def queued_events
52
- queue_js = generate_queue_js
53
- return nil if queue_js.blank?
54
- Tags.script_tag(queue_js)
59
+ if config.providers.kissmetrics.nil?
60
+ raise IscAnalytics::MissingConfigParams.new('KISSMetrics configuration isn\'t specified in your config.')
61
+ elsif config.providers.google_analytics.nil?
62
+ raise IscAnalytics::MissingConfigParams.new('Google Analytics configuration isn\'t specified in your config.')
63
+ end
55
64
  end
56
65
 
57
66
  def extend_analytics(namespace)
@@ -80,4 +89,4 @@ module IscAnalytics
80
89
  end
81
90
 
82
91
  end
83
- end
92
+ end
@@ -43,6 +43,22 @@ module IscAnalytics
43
43
  buffer
44
44
  end
45
45
 
46
+ def keep_km_url_event(params)
47
+ kme = params["kme"]
48
+
49
+ if kme
50
+ props = {}
51
+ params.each do |key, value|
52
+ if key.to_s.start_with?("km_")
53
+ prop_name = key[3..-1]
54
+ props[prop_name] = value
55
+ end
56
+ end
57
+
58
+ track_event(kme, props)
59
+ end
60
+ end
61
+
46
62
  private
47
63
 
48
64
  def enqueue(name, *args)
@@ -76,4 +92,4 @@ module IscAnalytics
76
92
  end
77
93
 
78
94
  end
79
- end
95
+ end
@@ -1,15 +1,15 @@
1
1
  module IscAnalytics
2
2
  class Config
3
3
 
4
- attr_accessor :accounts, :namespace
4
+ attr_accessor :providers, :namespace
5
5
 
6
- def self.default
7
- new.instance_eval {
8
- @accounts = nil
9
- @namespace = nil
6
+ def initialize(providers, namespace, amd=false)
7
+ @providers = providers
8
+ @namespace = namespace
9
+ end
10
10
 
11
- self
12
- }
11
+ def self.default
12
+ new(nil, nil)
13
13
  end
14
14
  end
15
- end
15
+ end
@@ -27,6 +27,10 @@ module IscAnalytics
27
27
  isc_analytics
28
28
  end
29
29
 
30
+ def keep_km_url_event
31
+ analytics.keep_km_url_event(params)
32
+ end
33
+
30
34
  private
31
35
 
32
36
  def isc_analytics
@@ -5,4 +5,4 @@ module IscAnalytics
5
5
  app.config.assets.precompile += %w(isc_analytics.js isc_analytics/session.js isc_analytics/opt_out_analytics.js)
6
6
  end
7
7
  end
8
- end
8
+ end
@@ -0,0 +1,3 @@
1
+ IscAnalytics::Engine.routes.draw do
2
+ get "track_mail" => "mail_analytics_controller#track"
3
+ end
@@ -0,0 +1,16 @@
1
+ module IscAnalytics
2
+ class MailAnalyticsController < ActionController::Base
3
+ def track
4
+ strategy = IscAnalytics::Mail.strategy
5
+ begin
6
+ if strategy.should_record_analytics?(params)
7
+ strategy.track(params[:identity], params[:kme], params[:props])
8
+ end
9
+ rescue => e
10
+ strategy.on_failure(e)
11
+ ensure
12
+ redirect_to params[:url]
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module IscAnalytics
2
+ class Mail
3
+ def track_url(url, event, props)
4
+ track_mail_analytics_url(url: url, event: event, props: props)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ require "isc_analytics/server_apis/kissmetrics"
2
+
3
+ module IscAnalytics
4
+ class Server
5
+ def initialize(providers_config)
6
+ @providers = []
7
+ @providers << init_kissmetrics(providers_config.kissmetrics)
8
+ end
9
+
10
+ def track_event(identity, name, properties={})
11
+ @providers.each do |provider|
12
+ provider.track_event(identity, name, properties)
13
+ end
14
+ end
15
+
16
+ def set_properties(identity, properties)
17
+ @providers.each do |provider|
18
+ provider.set_properties(identity, properties)
19
+ end
20
+ end
21
+
22
+ def alias(identity, new_identity)
23
+ @providers.each do |provider|
24
+ provider.alias(identity, new_identity)
25
+ end
26
+ end
27
+
28
+ private
29
+ def init_kissmetrics(config)
30
+ IscAnalytics::ServerApis::Kissmetrics.new(config["key"], config["dryrun"])
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'km'
2
+
3
+ module IscAnalytics
4
+ module ServerApis
5
+ class Kissmetrics
6
+ def initialize(key, dryrun=false)
7
+ raise ArgumentError.new("IscAnalytics::ServerApis::Kissmetrics requires a key") if key.blank?
8
+ KM.init(key, { :dryrun => dryrun })
9
+ end
10
+
11
+ def track_event(identity, event_name, properties={})
12
+ identify(identity)
13
+ KM.record(event_name, properties)
14
+ end
15
+
16
+ def set_properties(identity, properties)
17
+ identify(identity)
18
+ KM.set(properties)
19
+ end
20
+
21
+ def alias(original_identity, alias_identity)
22
+ identify(original_identity)
23
+ KM.alias(original_identity, alias_identity)
24
+ end
25
+
26
+ private
27
+ def identify(identity)
28
+ raise ArgumentError.new("identity must be provided") if identity.blank?
29
+ KM.identify(identity)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -4,13 +4,13 @@ require 'active_support/core_ext/object/blank'
4
4
  module IscAnalytics
5
5
  class Services
6
6
 
7
- def self.scripts(accounts)
8
- [
9
- kissmetrics_tag(accounts.kissmetrics_key),
10
- google_tag(accounts.google_analytics_key),
11
- optimizely_tag(accounts.optimizely_key),
12
- ipinfodb_tag(accounts.ipinfodb_key)
13
- ]
7
+ def self.scripts(providers_config)
8
+ scripts = []
9
+ scripts << kissmetrics_tag(providers_config.kissmetrics["key"]) if providers_config.kissmetrics
10
+ scripts << google_tag(providers_config.google_analytics["key"]) if providers_config.google_analytics
11
+ scripts << optimizely_tag(providers_config.optimizely["key"]) if providers_config.optimizely
12
+ scripts << ipinfodb_tag(providers_config.ipinfodb["key"]) if providers_config.ipinfodb
13
+ scripts
14
14
  end
15
15
 
16
16
  def self.kissmetrics_tag(key)
@@ -57,4 +57,4 @@ module IscAnalytics
57
57
  end
58
58
 
59
59
  end
60
- end
60
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: isc_analytics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.4
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-01-01 00:00:00.000000000 Z
13
+ date: 2013-02-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: controller_support
@@ -60,6 +60,22 @@ dependencies:
60
60
  - - ! '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: km
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
63
79
  description: A simple client-side & server-side analytics library
64
80
  email:
65
81
  - itayadler@gmail.com
@@ -68,26 +84,31 @@ executables: []
68
84
  extensions: []
69
85
  extra_rdoc_files: []
70
86
  files:
71
- - lib/assets/javascripts/isc_analytics/index.js.coffee
87
+ - lib/assets/javascripts/isc_analytics.js
72
88
  - lib/assets/javascripts/isc_analytics/isc_analytics.js.coffee
73
- - lib/assets/javascripts/isc_analytics/opt_out_analytics.js.coffee
74
89
  - lib/assets/javascripts/isc_analytics/visitor_analytics.coffee.erb
75
- - lib/assets/javascripts/isc_analytics.js
76
- - lib/isc_analytics/bootstrap.rb
77
- - lib/isc_analytics/client_api.rb
90
+ - lib/assets/javascripts/isc_analytics/opt_out_analytics.js.coffee
91
+ - lib/assets/javascripts/isc_analytics/index.js.coffee
92
+ - lib/isc_analytics.rb
78
93
  - lib/isc_analytics/config.rb
79
- - lib/isc_analytics/controller_support.rb
80
- - lib/isc_analytics/engine.rb
81
94
  - lib/isc_analytics/exceptions.rb
82
- - lib/isc_analytics/services.rb
83
95
  - lib/isc_analytics/tags.rb
84
- - lib/isc_analytics.rb
85
- - vendor/assets/javascripts/isc_analytics/session/index.js
86
- - vendor/assets/javascripts/isc_analytics/session/session-0.4.js
96
+ - lib/isc_analytics/mail/config/routes.rb
97
+ - lib/isc_analytics/mail/controllers/mail_analytics_controller.rb
98
+ - lib/isc_analytics/mail/mail.rb
99
+ - lib/isc_analytics/services.rb
100
+ - lib/isc_analytics/bootstrap.rb
101
+ - lib/isc_analytics/server.rb
102
+ - lib/isc_analytics/server_apis/kissmetrics.rb
103
+ - lib/isc_analytics/engine.rb
104
+ - lib/isc_analytics/client_api.rb
105
+ - lib/isc_analytics/controller_support.rb
87
106
  - vendor/assets/javascripts/isc_analytics/session.js
88
- - vendor/assets/javascripts/isc_analytics/yepnope/css_prefix.js
89
107
  - vendor/assets/javascripts/isc_analytics/yepnope/index.js
90
108
  - vendor/assets/javascripts/isc_analytics/yepnope/yepnope.js
109
+ - vendor/assets/javascripts/isc_analytics/yepnope/css_prefix.js
110
+ - vendor/assets/javascripts/isc_analytics/session/index.js
111
+ - vendor/assets/javascripts/isc_analytics/session/session-0.4.js
91
112
  - Rakefile
92
113
  - Gemfile
93
114
  - README.md