openpanel-sdk 0.1.0 → 0.1.2
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 +4 -4
- data/README.md +17 -9
- data/Rakefile +2 -2
- data/lib/openpanel/sdk/identify_user.rb +3 -1
- data/lib/openpanel/sdk/tracker.rb +35 -18
- data/lib/openpanel/sdk/version.rb +3 -1
- data/lib/openpanel/sdk.rb +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d14ac55d813c9ee97bcee346b856798bd52b1acac190a62945b4914f6588d1ad
|
|
4
|
+
data.tar.gz: 9bd22b7c57b950edb91b33c98c4653ad43dcbd18906dddda910fdb402481ec26
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 36be770f2140341f272334848831d667be6dbd44435e2aa6fa5a2ae1752fa76ee7b8c67bf348611c06b6b9817423fe3c11b5eb6835e61bdc0faa7d214ad7701b
|
|
7
|
+
data.tar.gz: 41be2ad0aeeb1ceb343e1fce8190591447bba27827bf0c7bbba2349e8ec2edcd0374ffd259bea6c30cffb5ea7f35735ec8fdea2322a087831df852f763df59f0
|
data/README.md
CHANGED
|
@@ -1,28 +1,36 @@
|
|
|
1
1
|
# Openpanel::SDK
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/openpanel/sdk`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
3
|
+
OpenPanel SDK for Ruby
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
|
-
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
|
10
|
-
|
|
11
7
|
Install the gem and add to the application's Gemfile by executing:
|
|
12
8
|
|
|
13
9
|
```bash
|
|
14
|
-
bundle add
|
|
10
|
+
bundle add openpanel-sdk
|
|
15
11
|
```
|
|
16
12
|
|
|
17
13
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
18
14
|
|
|
19
15
|
```bash
|
|
20
|
-
gem install
|
|
16
|
+
gem install openpanel-sdk
|
|
21
17
|
```
|
|
22
18
|
|
|
23
|
-
## Usage
|
|
19
|
+
## Usage example
|
|
20
|
+
|
|
21
|
+
Use the gem by adding the following line to the Gemfile:
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
```ruby
|
|
24
|
+
tracker = OpenPanel::SDK::Tracker.new
|
|
25
|
+
identify_user = OpenPanel::SDK::IdentifyUser.new
|
|
26
|
+
|
|
27
|
+
# ... set user props
|
|
28
|
+
|
|
29
|
+
tracker.identify identify_user
|
|
30
|
+
tracker.track 'test_event', payload: { name: 'test' }
|
|
31
|
+
tracker.increment_property identify_user, 'test_property', 1
|
|
32
|
+
tracker.decrement_property identify_user, 'test_property', 1
|
|
33
|
+
```
|
|
26
34
|
|
|
27
35
|
## Development
|
|
28
36
|
|
data/Rakefile
CHANGED
|
@@ -1,26 +1,30 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'faraday'
|
|
2
4
|
|
|
3
5
|
module OpenPanel
|
|
4
6
|
module SDK
|
|
5
7
|
class OpenPanelError < StandardError; end
|
|
6
8
|
|
|
7
9
|
class Tracker
|
|
8
|
-
TRACKING_TYPE_IDENTIFY =
|
|
9
|
-
TRACKING_TYPE_TRACK =
|
|
10
|
-
TRACKING_TYPE_INCREMENT =
|
|
11
|
-
TRACKING_TYPE_DECREMENT =
|
|
10
|
+
TRACKING_TYPE_IDENTIFY = 'identify'
|
|
11
|
+
TRACKING_TYPE_TRACK = 'track'
|
|
12
|
+
TRACKING_TYPE_INCREMENT = 'increment'
|
|
13
|
+
TRACKING_TYPE_DECREMENT = 'decrement'
|
|
12
14
|
|
|
13
15
|
attr_reader :headers
|
|
14
16
|
|
|
15
17
|
##############################
|
|
16
18
|
# Initialize OpenPanel tracker
|
|
19
|
+
# @param disabled [Boolean] disable sending tracking events, defaults to false
|
|
17
20
|
##############################
|
|
18
|
-
def initialize
|
|
21
|
+
def initialize(disabled: false)
|
|
19
22
|
@headers = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
'Content-Type' => 'application/json',
|
|
24
|
+
'openpanel-client-id' => ENV['OPENPANEL_CLIENT_ID'],
|
|
25
|
+
'openpanel-client-secret' => ENV['OPENPANEL_CLIENT_SECRET']
|
|
23
26
|
}
|
|
27
|
+
@disabled = disabled
|
|
24
28
|
end
|
|
25
29
|
|
|
26
30
|
##############################
|
|
@@ -52,9 +56,9 @@ module OpenPanel
|
|
|
52
56
|
##############################
|
|
53
57
|
def track_page_view(user, path)
|
|
54
58
|
if user
|
|
55
|
-
track
|
|
59
|
+
track 'view', payload: { profileId: user.profile_id, path: path }
|
|
56
60
|
else
|
|
57
|
-
track
|
|
61
|
+
track 'view', payload: { path: path }
|
|
58
62
|
end
|
|
59
63
|
end
|
|
60
64
|
|
|
@@ -64,13 +68,20 @@ module OpenPanel
|
|
|
64
68
|
# @param user [OpenPanel::SDK::IdentifyUser] user to identify
|
|
65
69
|
##############################
|
|
66
70
|
def identify(user)
|
|
67
|
-
payload = { profileId: user.profile_id, firstName: user.first_name, lastName: user.last_name,
|
|
71
|
+
payload = { profileId: user.profile_id, firstName: user.first_name, lastName: user.last_name,
|
|
72
|
+
email: user.email }
|
|
68
73
|
payload = { type: TRACKING_TYPE_IDENTIFY, payload: payload }
|
|
69
74
|
|
|
70
75
|
send_request payload: payload
|
|
71
76
|
end
|
|
72
77
|
|
|
73
|
-
|
|
78
|
+
##############################
|
|
79
|
+
# Decrement property in OpenPanel
|
|
80
|
+
# @param user [User] user to identify
|
|
81
|
+
# @param property [String] property to increment
|
|
82
|
+
# @param value [Integer] value to increment by
|
|
83
|
+
#############################
|
|
84
|
+
def increment_property(user, property = 'visits', value = 1)
|
|
74
85
|
payload = { profileId: user.profile_id, property: property, value: value }
|
|
75
86
|
payload = { type: TRACKING_TYPE_INCREMENT, payload: payload }
|
|
76
87
|
|
|
@@ -80,8 +91,10 @@ module OpenPanel
|
|
|
80
91
|
##############################
|
|
81
92
|
# Decrement property in OpenPanel
|
|
82
93
|
# @param user [User] user to identify
|
|
94
|
+
# @param property [String] property to increment
|
|
95
|
+
# @param value [Integer] value to decrement by
|
|
83
96
|
#############################
|
|
84
|
-
def decrement_property(user, property =
|
|
97
|
+
def decrement_property(user, property = 'visits', value = 1)
|
|
85
98
|
payload = { profileId: user.profile_id, property: property, value: value }
|
|
86
99
|
payload = { type: TRACKING_TYPE_DECREMENT, payload: payload }
|
|
87
100
|
|
|
@@ -94,17 +107,21 @@ module OpenPanel
|
|
|
94
107
|
# Send request to OpenPanel API
|
|
95
108
|
# @param url [String] API endpoint URL
|
|
96
109
|
# @param payload [Hash] request payload
|
|
110
|
+
# @return [Faraday::Response] The Faraday plain response object
|
|
111
|
+
# @raise [OpenPanel::SDK::OpenPanelError] if request fails
|
|
97
112
|
##############################
|
|
98
|
-
def send_request(url: ENV[
|
|
113
|
+
def send_request(url: ENV['OPENPANEL_TRACK_URL'], payload: {})
|
|
114
|
+
return if @disabled
|
|
115
|
+
|
|
99
116
|
response = Faraday.post url, payload.to_json, @headers
|
|
100
117
|
|
|
101
118
|
case response.status
|
|
102
119
|
when 401
|
|
103
|
-
raise OpenPanel::SDK::OpenPanelError,
|
|
120
|
+
raise OpenPanel::SDK::OpenPanelError, 'Unauthorized'
|
|
104
121
|
when 429
|
|
105
|
-
raise OpenPanel::SDK::OpenPanelError,
|
|
122
|
+
raise OpenPanel::SDK::OpenPanelError, 'Too many requests'
|
|
106
123
|
when 500
|
|
107
|
-
raise OpenPanel::SDK::OpenPanelError,
|
|
124
|
+
raise OpenPanel::SDK::OpenPanelError, 'Internal server error'
|
|
108
125
|
else
|
|
109
126
|
response
|
|
110
127
|
end
|
data/lib/openpanel/sdk.rb
CHANGED