thisdata 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +4 -0
- data/README.md +41 -12
- data/lib/generators/this_data/install_generator.rb +3 -0
- data/lib/this_data.rb +38 -9
- data/lib/this_data/configuration.rb +6 -0
- data/lib/this_data/track_request.rb +12 -26
- data/lib/this_data/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 824e016111d0ad0979a53296fbeaf9020e0d44fe
|
4
|
+
data.tar.gz: 88001169e0964ed9068c7d43f524df35e5740acb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cf37586dfcb83cf7d6c12256f68767b5d5ac0c0de3e90fd7208d64a4413c5c60a6183325f94912adb8636436b902c5779e01c605738e4517198fb03831dc1fe
|
7
|
+
data.tar.gz: 681c7e15c374b83551ff7d511eafd6fcc691fedf3249c316860d4bdb3e3aaff5ce9a7416ccf35336979afa54f8cf4f7de44eb96b91d4c2be21fd1b10322b282a
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -20,25 +20,37 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
### Ruby
|
24
24
|
|
25
|
-
|
25
|
+
Configure ThisData as follows:
|
26
|
+
|
27
|
+
```
|
28
|
+
ThisData.setup do |config|
|
29
|
+
config.api_key = "API_KEY_HERE"
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
You can then track any event by calling `ThisData.track` and passing a Hash which
|
34
|
+
contains an event. See examples and required fields on our API documentation:
|
35
|
+
http://help.thisdata.com/docs/apiv1events
|
36
|
+
|
37
|
+
**Important!** You should not commit your API keys to source control. Where
|
38
|
+
possible, use environment variables / a non-committed secrets file / something
|
39
|
+
similar.
|
40
|
+
|
41
|
+
### Rails
|
26
42
|
|
27
|
-
|
43
|
+
Find your API key by going to [ThisData](https://thisdata.com) >
|
28
44
|
Integrations > Login Intelligence API.
|
29
45
|
|
46
|
+
Run:
|
47
|
+
|
48
|
+
rails g this_data:install YOUR_API_KEY_HERE
|
49
|
+
|
30
50
|
The generator will create a file in `config/initializers` called "this_data.rb".
|
31
51
|
If you need to do any further configuration or customization of ThisData,
|
32
52
|
that's the place to do it!
|
33
53
|
|
34
|
-
## Ruby
|
35
|
-
|
36
|
-
You can track any event by calling `ThisData.track` and passing a Hash which
|
37
|
-
contains an event. See examples and required fields on our API documentation:
|
38
|
-
http://help.thisdata.com/docs/apiv1events
|
39
|
-
|
40
|
-
## Rails
|
41
|
-
|
42
54
|
The ThisData::TrackRequest module can be included in a ActionController, giving
|
43
55
|
you a handy way to track requests.
|
44
56
|
|
@@ -67,7 +79,24 @@ class SessionsController < ApplicationController
|
|
67
79
|
end
|
68
80
|
```
|
69
81
|
|
82
|
+
### Stuck?
|
83
|
+
|
84
|
+
By default there is no logger configured, and requests are performed
|
85
|
+
asynchronously. The following config settings can be helpful in debugging issues:
|
86
|
+
|
87
|
+
`config/initializers/this_data.rb`
|
88
|
+
```
|
89
|
+
ThisData.setup do |config|
|
90
|
+
# ...
|
91
|
+
|
92
|
+
config.logger = Rails.logger # or Logger.new($stdout)
|
93
|
+
config.asyc = false
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
Our documentation can be read at http://help.thisdata.com.
|
70
98
|
|
99
|
+
Reach out to developers@thisdata.com if you need any help.
|
71
100
|
|
72
101
|
## Development
|
73
102
|
|
@@ -77,4 +106,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
77
106
|
|
78
107
|
## Contributing
|
79
108
|
|
80
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
109
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thisdata/thisdata-ruby.
|
@@ -22,6 +22,9 @@ ThisData.setup do |config|
|
|
22
22
|
|
23
23
|
# Define a Logger instance if you want to debug / track errors
|
24
24
|
# config.logger = Rails.logger unless Rails.env.production?
|
25
|
+
|
26
|
+
# Set this to false if you want ThisData.track to perform in the same thread
|
27
|
+
# config.asyc = false
|
25
28
|
end
|
26
29
|
EOS
|
27
30
|
end
|
data/lib/this_data.rb
CHANGED
@@ -27,18 +27,17 @@ module ThisData
|
|
27
27
|
configuration.defaults
|
28
28
|
end
|
29
29
|
|
30
|
-
#
|
30
|
+
# Tracks an event. If `ThisData.configuration.async` is true, this action
|
31
|
+
# will be performed in a new Thread.
|
31
32
|
# Event must be a Hash
|
33
|
+
# When performed asynchronously, true is always returned.
|
34
|
+
# Otherwise an HTTPRequest is returned.
|
32
35
|
def track(event)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
ThisData.error(e)
|
38
|
-
e.backtrace.each do |line|
|
39
|
-
ThisData.error(line, prefix: false)
|
36
|
+
if ThisData.configuration.async
|
37
|
+
track_async(event)
|
38
|
+
else
|
39
|
+
track_with_response(event)
|
40
40
|
end
|
41
|
-
false
|
42
41
|
end
|
43
42
|
|
44
43
|
# A helper method to track a log-in event. Validates that the minimum
|
@@ -67,5 +66,35 @@ module ThisData
|
|
67
66
|
log(message, level: 'error', prefix: prefix)
|
68
67
|
end
|
69
68
|
|
69
|
+
private
|
70
|
+
|
71
|
+
# Creates a Client and tracks an event.
|
72
|
+
# Event must be a Hash.
|
73
|
+
# Rescues and logs all exceptions.
|
74
|
+
# Returns an HTTPResponse
|
75
|
+
def track_with_response(event)
|
76
|
+
response = Client.new.track(event)
|
77
|
+
log("Tracked event!")
|
78
|
+
response
|
79
|
+
rescue => e
|
80
|
+
ThisData.error("Failed to track event:")
|
81
|
+
ThisData.error(e)
|
82
|
+
e.backtrace.each do |line|
|
83
|
+
ThisData.error(line, prefix: false)
|
84
|
+
end
|
85
|
+
false
|
86
|
+
end
|
87
|
+
|
88
|
+
# Performs the track function within a new Thread, so it is non blocking.
|
89
|
+
# Returns the Thread created
|
90
|
+
def track_async(event)
|
91
|
+
Thread.new do
|
92
|
+
track_with_response(event)
|
93
|
+
end
|
94
|
+
rescue => e
|
95
|
+
ThisData.error("Cannot create Thread: #{e.inspect}")
|
96
|
+
false
|
97
|
+
end
|
98
|
+
|
70
99
|
end
|
71
100
|
end
|
@@ -18,6 +18,11 @@ module ThisData
|
|
18
18
|
# > Login Intelligence API
|
19
19
|
config_option :api_key
|
20
20
|
|
21
|
+
# When true, requests will be performed asynchronously. Setting this to
|
22
|
+
# false can help with debugging.
|
23
|
+
# Default: true
|
24
|
+
config_option :async
|
25
|
+
|
21
26
|
# Log the events sent
|
22
27
|
config_option :logger
|
23
28
|
|
@@ -41,6 +46,7 @@ module ThisData
|
|
41
46
|
|
42
47
|
# set default attribute values
|
43
48
|
@defaults = OpenStruct.new({
|
49
|
+
async: true,
|
44
50
|
user_method: :current_user,
|
45
51
|
user_id_method: :id,
|
46
52
|
user_name_method: :name,
|
@@ -1,6 +1,10 @@
|
|
1
1
|
# Include ThisData::TrackRequest in your ApplicationController to get a handy
|
2
2
|
# track method which looks at the request and current_user variables to
|
3
3
|
# generate an event.
|
4
|
+
#
|
5
|
+
# If you include this in a non-ActionController instance, you must respond to
|
6
|
+
# `request` and `ThisData.configuration.user_method`
|
7
|
+
#
|
4
8
|
module ThisData
|
5
9
|
module TrackRequest
|
6
10
|
class ThisDataTrackError < StandardError; end
|
@@ -8,47 +12,29 @@ module ThisData
|
|
8
12
|
# Will pull request and user details from the controller, and send an event
|
9
13
|
# to ThisData.
|
10
14
|
def thisdata_track(verb: ThisData::Verbs::LOG_IN)
|
11
|
-
controller = controller_from_env
|
12
|
-
user_details = user_details_from_controller(controller)
|
13
15
|
event = {
|
14
|
-
verb:
|
15
|
-
ip:
|
16
|
+
verb: verb,
|
17
|
+
ip: request.remote_ip,
|
16
18
|
user_agent: request.user_agent,
|
17
|
-
user:
|
19
|
+
user: user_details
|
18
20
|
}
|
19
21
|
|
20
22
|
ThisData.track(event)
|
21
23
|
rescue => e
|
22
24
|
ThisData.error "Could not track event:"
|
23
25
|
ThisData.error e
|
24
|
-
ThisData.error e.backtrace
|
26
|
+
ThisData.error e.backtrace[0..5].join("\n")
|
25
27
|
false
|
26
28
|
end
|
27
29
|
|
28
30
|
private
|
29
31
|
|
30
|
-
#
|
31
|
-
#
|
32
|
-
def controller_from_env
|
33
|
-
unless controller = env["action_controller.instance"]
|
34
|
-
raise ThisDataTrackError, "Could not get controller from environment"
|
35
|
-
end
|
36
|
-
controller
|
37
|
-
end
|
38
|
-
|
39
|
-
# Fetches the user record by calling the configured method on the
|
40
|
-
# controller.
|
41
|
-
# Will raise a NoMethodError if controller does not respond to the method.
|
42
|
-
# Returns an object
|
43
|
-
def user_from_controller(controller)
|
44
|
-
user = controller.send(ThisData.configuration.user_method)
|
45
|
-
end
|
46
|
-
|
47
|
-
# Will fetch a user and return a Hash of details for that User.
|
32
|
+
# Will fetch a user and return a Hash of details for the User returned
|
33
|
+
# by `ThisData.configuration.user_method`.
|
48
34
|
# Will raise a NoMethodError if controller does not return a user,
|
49
35
|
# or we can't get a user id.
|
50
|
-
def
|
51
|
-
user =
|
36
|
+
def user_details
|
37
|
+
user = send(ThisData.configuration.user_method)
|
52
38
|
{
|
53
39
|
id: user.send(ThisData.configuration.user_id_method),
|
54
40
|
name: value_if_configured(user, "user_name_method"),
|
data/lib/this_data/version.rb
CHANGED