clearbit 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e0b00b7a895da02d3d5ca094f8960a954b5ecde28891979a515015608c2fd6d
4
- data.tar.gz: e4e411ccc729b972cf7139594e59da959ff22222f82fb53e312c1b529512e7ee
3
+ metadata.gz: 1ef2fb93ad9465fc8f69a87a2b94ead8843082586e763bbe3d32d2ca8a20cc57
4
+ data.tar.gz: 1ae24030ff38ee37a7079c5b47cfe859233c1e613c515e5f84fcca3b0b1aed4f
5
5
  SHA512:
6
- metadata.gz: 368ac6459e7e2613e2636731fef9db010d3ae30c9772b7d57017c24d96b4760c9a1af6cddfcfbe6b793051e07552194c519d4a201883f04ec861185b8d5e475a
7
- data.tar.gz: '08f475f7c73ccd1c77dd5b660f9c061bc1531402e1d982cf8f2142d5094fe7354087da8a27f50ed30e9562feb29c67b607495e7399d366e1d480b1915f150efa'
6
+ metadata.gz: 668f7320b3f7774a2712ace35d08e264dfe6095039db4dfe169bdad19e5613f739add95ffe4a1b95af2583abd9c403a27e9a822abdda867ce85842f40667ac05
7
+ data.tar.gz: a6955e0c932d7990c9f1ba0a02735f4fdfb7fa75c1e6d50cd3b2ac15bd113bbbd8af21303a3b2ec306af76c91ff28b1ea9ecaf204bf01a4d026e2fd37995f640
data/README.md CHANGED
@@ -80,16 +80,46 @@ See the [documentation](https://clearbit.com/docs#company-api) for more informat
80
80
 
81
81
  ## Analytics
82
82
 
83
+ *NOTE:* We **strongly** recommend using `clearbit.js` for Analytics and integrating with Clearbit X. It handles a lot of complexity, like generating `anonymous_id`s and associating them with `user_id`s when a user is identified. It also automatically tracks `page` views with the full data set.
84
+
85
+ ### Identifying Users
86
+
83
87
  Identify users by sending their `user_id`, and adding details like their `email` and `company_domain` to create People and Companies inside of Clearbit X.
84
88
 
85
89
  ```ruby
86
90
  Clearbit::Analytics.identify(
87
- user_id: '1234', # Required
91
+ user_id: '1234', # Required if no anonymous_id is sent. The user's ID in your database.
92
+ anonymous_id: session[:anonymous_id], # Required if no user_id is sent. A UUID to track anonymous users.
88
93
  traits: {
89
94
  email: 'david@clearbitexample.com', # Optional, but strongly recommended
90
95
  company_domain: 'clearbit.com', # Optional, but strongly recommended
91
96
  first_name: 'David', # Optional
92
97
  last_name: 'Lumley', # Optional
98
+ # … other analytical traits can also be sent, like the plan a user is on etc
99
+ },
100
+ context: {
101
+ ip: '89.102.33.1' # Optional, but strongly recommended when identifying users
102
+ } # as they sign up, or log in
103
+ )
104
+ ```
105
+
106
+ ### Page Views
107
+
108
+ Use the `page` method, and send the users `anonymous_id` along with the `url` they're viewing, and the `ip` the request comes from in order to create Companies inside of Clearbit X and track their page views.
109
+
110
+ ```ruby
111
+ Clearbit::Analytics.page(
112
+ user_id: '1234', # Required if no anonymous_id is sent. The user's ID in your database.
113
+ anonymous_id: session[:anonymous_id], # Required if no user_id is sent. A UUID to track anonymous users.
114
+ name: 'Clearbit Ruby Library', # Optional, but strongly recommended
115
+ properties: {
116
+ url: 'https://github.com/clearbit/clearbit-ruby?utm_source=google', # Required. Likely to be request.referer
117
+ path: '/clearbit/clearbit-ruby', # Optional, but strongly recommended
118
+ search: '?utm_source=google', # Optional, but strongly recommended
119
+ referrer: nil, # Optional. Unlikely to be request.referrer.
120
+ },
121
+ context: {
122
+ ip: '89.102.33.1', # Optional, but strongly recommended.
93
123
  },
94
124
  )
95
125
  ```
@@ -17,6 +17,14 @@ module Clearbit
17
17
  analytics.flush
18
18
  end
19
19
 
20
+ # Proxy page through to a client instance, in order to keep the client
21
+ # consistent with how the other Clearbit APIs are accessed
22
+ def self.page(args)
23
+ analytics = new(write_key: Clearbit.key)
24
+ analytics.page(args)
25
+ analytics.flush
26
+ end
27
+
20
28
  # Initializes a new instance of {Clearbit::Analytics::Client}, to which all
21
29
  # method calls are proxied.
22
30
  #
@@ -0,0 +1,114 @@
1
+ # Getting Started with Clearbit::Analytics
2
+
3
+ ## Initialization
4
+
5
+ Make sure you set your `Clearbit.key` as part of your apps initialization, or before you make use of `Clearbit::Analytics`. You can find your secret key at [https://dashboard.clearbit.com/api](https://dashboard.clearbit.com/api)
6
+
7
+ `Clearbit.key = 'sk_…'`.
8
+
9
+ We also recommend ensuring that every person has a randomly generated `anonymous_id` to aid tracking page events and assist in matching up anonymous traffic to signed in users.
10
+
11
+ ```ruby
12
+ class ApplicationController
13
+ before_action :set_anonymous_id
14
+
15
+
16
+ def set_anonymous_id
17
+ session[:anonymous_id] ||= SecureRandom.uuid
18
+ end
19
+
20
+
21
+ end
22
+ ```
23
+
24
+ ## Importing Users into X
25
+
26
+ You'll want to loop through your users, and call `.identify` for each one, passing the users `id`, and their `email` along with any other traits you value.
27
+
28
+ ```ruby
29
+ User.find_in_batches do |users|
30
+ users.each do |user|
31
+ Clearbit::Analytics.identify(
32
+ user_id: user.id, # Required.
33
+ traits: {
34
+ email: user.email, # Required.
35
+ company_domain: user.domain || user.email.split('@').last, # Optional, strongly recommended.
36
+ first_name: user.first_name, # Optional.
37
+ last_name: user.last_name, # Optional.
38
+ # … other analytical traits can also be sent, like the plan a user is on etc.
39
+ },
40
+ )
41
+ end
42
+ end
43
+ ```
44
+
45
+ ## Identifying Users on sign up / log in
46
+
47
+ Identifying users on sign up, or on log in will help Clearbit X associate the user with anonymous page views.
48
+
49
+ ```ruby
50
+ class SessionsController
51
+
52
+
53
+ def create
54
+
55
+
56
+ identify(current_user)
57
+
58
+
59
+ end
60
+
61
+ private
62
+
63
+ def identify(user)
64
+ Clearbit::Analytics.identify(
65
+ user_id: user.id, # Required
66
+ anonymous_id: session[:anonymous_id], # Optional, strongly recommended. Helps Clearbit X associate with anonymous visits.
67
+ traits: {
68
+ email: user.email, # Required.
69
+ company_domain: user.domain || user.email.split('@').last, # Optional, strongly recommended.
70
+ first_name: user.first_name, # Optional.
71
+ last_name: user.last_name, # Optional.
72
+ # … other analytical traits can also be sent, like the plan a user is on etc.
73
+ },
74
+ context: {
75
+ ip: request.ip, # Optional, but strongly recommended. Helps Clearbit X associate with anonymous visits.
76
+ },
77
+ )
78
+ end
79
+
80
+
81
+ end
82
+ ```
83
+
84
+ ## Sending Page views
85
+
86
+ Tracking page views is best done using a `anonymous_id` you generate for each user. You'll need to sent the `url` as part of the `properties` hash, and the `ip` as part of the `context` hash. Any other data you can provide will greatly help with segmentation inside of Clearbit X.
87
+
88
+
89
+ ```ruby
90
+ uri = URI(request.referer) # You'll likely want to do some error handling here.
91
+
92
+ Clearbit::Analytics.page(
93
+ user_id: current_user&.id, # Optional
94
+ anonymous_id: session[:anonymous_id], # Required
95
+ properties: {
96
+ url: request.referrer, # Required. Because this is a backend integration, the referrer is the URL that was visited.
97
+ path: format_path(uri.path), # Optional, but strongly recommended.
98
+ search: format_search(uri.params), # Optional, but strongly recommended.
99
+ referrer: nil, # Optional. Not the `request.referer`, but the referrer of the original page view.
100
+ },
101
+ context: {
102
+ ip: request.ip, # Required. Helps Clearbit X associate anonymous visits with Companies.
103
+ },
104
+ )
105
+
106
+ def format_path(path)
107
+ path.presence || '/'
108
+ end
109
+
110
+ def format_search(search)
111
+ return unless search
112
+ '?' + search
113
+ end
114
+ ```
@@ -170,7 +170,7 @@ module Clearbit
170
170
  end
171
171
 
172
172
  def add_context!(context)
173
- context[:library] = { :name => 'analytics-ruby', :version => Clearbit::VERSION.to_s }
173
+ context[:library] = { :name => 'clearbit-ruby', :version => Clearbit::VERSION.to_s }
174
174
  end
175
175
 
176
176
  # private: Ensures that a string is non-empty
@@ -38,7 +38,7 @@ module Clearbit
38
38
  logger.progname = 'Clearbit::Analytics'
39
39
  logger
40
40
  end
41
- @logger = PrefixedLogger.new(base_logger, '[analytics-ruby]')
41
+ @logger = PrefixedLogger.new(base_logger, '[clearbit-ruby]')
42
42
  end
43
43
 
44
44
  attr_writer :logger
@@ -1,3 +1,3 @@
1
1
  module Clearbit
2
- VERSION = '0.3.0'
2
+ VERSION = '0.3.1'
3
3
  end
@@ -6,20 +6,41 @@ describe Clearbit::Analytics do
6
6
  Clearbit.key = 'clearbit_key'
7
7
  end
8
8
 
9
- it 'sends an identify request to Clearbit X' do
10
- basic_auth = Base64.encode64('clearbit_key:').strip
11
- x_stub = stub_request(:post, 'https://x.clearbit.com/v1/import').
12
- with(
13
- headers: { 'Authorization' => "Basic #{basic_auth}" }
14
- ).to_return(status: 200, body: {success: true}.to_json)
9
+ describe '#identify' do
10
+ it 'sends an identify request to Clearbit X' do
11
+ basic_auth = Base64.encode64('clearbit_key:').strip
12
+ x_stub = stub_request(:post, 'https://x.clearbit.com/v1/import').
13
+ with(
14
+ headers: { 'Authorization' => "Basic #{basic_auth}" }
15
+ ).to_return(status: 200, body: {success: true}.to_json)
15
16
 
16
- Clearbit::Analytics.identify(
17
- user_id: '123',
18
- traits: {
19
- email: 'david@clearbit.com',
20
- },
21
- )
17
+ Clearbit::Analytics.identify(
18
+ user_id: '123',
19
+ traits: {
20
+ email: 'david@clearbit.com',
21
+ },
22
+ )
22
23
 
23
- expect(x_stub).to have_been_requested
24
+ expect(x_stub).to have_been_requested
25
+ end
26
+ end
27
+
28
+ describe '#page' do
29
+ it 'sends an identify request to Clearbit X' do
30
+ basic_auth = Base64.encode64('clearbit_key:').strip
31
+ x_stub = stub_request(:post, 'https://x.clearbit.com/v1/import').
32
+ with(
33
+ headers: { 'Authorization' => "Basic #{basic_auth}" }
34
+ ).to_return(status: 200, body: {success: true}.to_json)
35
+
36
+ Clearbit::Analytics.page(
37
+ user_id: '123',
38
+ traits: {
39
+ email: 'david@clearbit.com',
40
+ },
41
+ )
42
+
43
+ expect(x_stub).to have_been_requested
44
+ end
24
45
  end
25
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clearbit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex MacCaw
@@ -151,6 +151,7 @@ files:
151
151
  - lib/clearbit.rb
152
152
  - lib/clearbit/analytics.rb
153
153
  - lib/clearbit/analytics/LICENSE
154
+ - lib/clearbit/analytics/README.md
154
155
  - lib/clearbit/analytics/backoff_policy.rb
155
156
  - lib/clearbit/analytics/client.rb
156
157
  - lib/clearbit/analytics/defaults.rb