openpanel-sdk 0.3.3 → 0.3.4
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/.junie/AGENTS.md +67 -0
- data/lib/openpanel/sdk/group.rb +30 -0
- data/lib/openpanel/sdk/tracker.rb +40 -6
- data/lib/openpanel/sdk/version.rb +1 -1
- data/lib/openpanel/sdk.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c159e40267698c580c68f4eb194e3d84bad06967c89157402cc34f56b831d350
|
|
4
|
+
data.tar.gz: 0427f3b059c49f4df136875d93d65c99dd01625f370c7f8560d1edc45a935c74
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: eb4f828530306445f30c1de85ec402fb09346bd19d8afbc146eb7ee97d318f57dc2d545dbe11c406408bb97bc0ad7a1bac8fb989fd3aeaf41ea6f5c86faa4d90
|
|
7
|
+
data.tar.gz: a3465621ab65ada9a9c1f3f503507a5bb44dc702f09cb97bf992f0b185c041487e2522413c68dbb5aa26c8115a92c94fcd4cc459b38c874778e24a78dffbc5d1
|
data/.junie/AGENTS.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Development Guidelines for OpenPanel SDK
|
|
2
|
+
|
|
3
|
+
This document provides project-specific information for developers working on the OpenPanel Ruby SDK.
|
|
4
|
+
|
|
5
|
+
## Build and Configuration
|
|
6
|
+
|
|
7
|
+
### Environment Setup
|
|
8
|
+
1. **Dependencies**: Install the required gems using Bundler:
|
|
9
|
+
```bash
|
|
10
|
+
bundle install
|
|
11
|
+
```
|
|
12
|
+
2. **Environment Variables**: The project uses `dotenv` to manage environment variables. Create a `.env` file in the root directory based on `.env_sample`:
|
|
13
|
+
```env
|
|
14
|
+
OPENPANEL_TRACK_URL=https://api.openpanel.dev/track
|
|
15
|
+
OPENPANEL_CLIENT_ID=<YOUR_CLIENT_ID>
|
|
16
|
+
OPENPANEL_CLIENT_SECRET=<YOUR_CLIENT_SECRET>
|
|
17
|
+
```
|
|
18
|
+
*Note: Ensure these variables are set correctly for the tracker to authenticate with the OpenPanel API.*
|
|
19
|
+
|
|
20
|
+
### SDK Initialization
|
|
21
|
+
The `OpenPanel::SDK::Tracker` requires `OPENPANEL_CLIENT_ID` and `OPENPANEL_CLIENT_SECRET` to be present in the environment if events are to be sent. You can disable event sending for testing purposes by passing `disabled: true` to the initializer.
|
|
22
|
+
|
|
23
|
+
## Testing
|
|
24
|
+
|
|
25
|
+
### Running Tests
|
|
26
|
+
The project uses RSpec. To run the full test suite:
|
|
27
|
+
```bash
|
|
28
|
+
bundle exec rspec
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Adding New Tests
|
|
32
|
+
When adding new features or fixing bugs, add corresponding specs in the `spec/` directory. Ensure you require `spec_helper` at the top of your spec file.
|
|
33
|
+
|
|
34
|
+
### Simple Test Example
|
|
35
|
+
Below is a basic test demonstrating how to verify the SDK version and tracker initialization without making network requests:
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
# spec/example_spec.rb
|
|
39
|
+
require 'spec_helper'
|
|
40
|
+
|
|
41
|
+
RSpec.describe 'SDK Setup' do
|
|
42
|
+
it 'verifies version is defined' do
|
|
43
|
+
expect(OpenPanel::SDK::VERSION).not_to be_nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it 'initializes tracker in disabled mode' do
|
|
47
|
+
tracker = OpenPanel::SDK::Tracker.new({ app: 'test' }, disabled: true)
|
|
48
|
+
expect(tracker.global_properties[:app]).to eq('test')
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Additional Development Information
|
|
54
|
+
|
|
55
|
+
### HTTP Client
|
|
56
|
+
The SDK uses **Faraday** for HTTP communication. The `Tracker` class handles the request logic, including basic error handling for common HTTP status codes (401, 429, 500).
|
|
57
|
+
|
|
58
|
+
### Code Style
|
|
59
|
+
- Follow standard Ruby naming conventions (snake_case for methods/variables, PascalCase for classes/modules).
|
|
60
|
+
- The project includes `rubocop` for linting. You can run it with:
|
|
61
|
+
```bash
|
|
62
|
+
bundle exec rubocop
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Key Components
|
|
66
|
+
- `OpenPanel::SDK::Tracker`: Main entry point for tracking events, users, and revenue.
|
|
67
|
+
- `OpenPanel::SDK::IdentifyUser`: Data class for user identification properties.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OpenPanel
|
|
4
|
+
module SDK
|
|
5
|
+
# Group payload class. Use this class to create or update a group in OpenPanel.
|
|
6
|
+
# @attr id [String] group ID
|
|
7
|
+
# @attr type [String] group type (e.g. 'company', 'team')
|
|
8
|
+
# @attr name [String] group name
|
|
9
|
+
# @attr properties [Hash] group properties
|
|
10
|
+
class Group
|
|
11
|
+
attr_accessor :id, :type, :name, :properties
|
|
12
|
+
|
|
13
|
+
def initialize(id: nil, type: nil, name: nil, properties: {})
|
|
14
|
+
@id = id
|
|
15
|
+
@type = type
|
|
16
|
+
@name = name
|
|
17
|
+
@properties = properties
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def to_json(*_args)
|
|
21
|
+
{
|
|
22
|
+
id: id,
|
|
23
|
+
type: type,
|
|
24
|
+
name: name,
|
|
25
|
+
properties: properties
|
|
26
|
+
}.to_json
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -13,6 +13,8 @@ module OpenPanel
|
|
|
13
13
|
TRACKING_TYPE_TRACK = 'track'
|
|
14
14
|
TRACKING_TYPE_INCREMENT = 'increment'
|
|
15
15
|
TRACKING_TYPE_DECREMENT = 'decrement'
|
|
16
|
+
TRACKING_TYPE_GROUP = 'group'
|
|
17
|
+
TRACKING_TYPE_ASSIGN_GROUP = 'assign_group'
|
|
16
18
|
|
|
17
19
|
attr_reader :headers, :global_properties
|
|
18
20
|
|
|
@@ -42,14 +44,17 @@ module OpenPanel
|
|
|
42
44
|
# @param profile_id [String] (optional) user profile ID
|
|
43
45
|
# @param tracking_type [String]
|
|
44
46
|
# @param payload [Hash] event payload
|
|
47
|
+
# @param groups [Array<String>] (optional) list of group IDs
|
|
45
48
|
# @param filter [Lambda] pass in a lambda to filter events. If the lambda returns true, the event won't be tracked.
|
|
46
49
|
# @return [Faraday::Response | nil] The Faraday plain response object or nil, if the event was filtered out
|
|
47
50
|
# @raise [OpenPanel::SDK::OpenPanelError] if the request fails or the event filter is not a method or lambda
|
|
48
|
-
def track(event, profile_id: '', tracking_type: TRACKING_TYPE_TRACK, payload: {}, filter: ->(_payload) { false })
|
|
51
|
+
def track(event, profile_id: '', tracking_type: TRACKING_TYPE_TRACK, payload: {}, groups: nil, filter: ->(_payload) { false })
|
|
49
52
|
return if filter.call(payload)
|
|
50
53
|
|
|
51
54
|
payload = global_properties.merge(payload) unless global_properties.empty?
|
|
52
|
-
payload = {
|
|
55
|
+
payload = { name: event, profileId: profile_id, properties: payload }
|
|
56
|
+
payload[:groups] = Array(groups) if groups
|
|
57
|
+
payload = { type: tracking_type, payload: payload }
|
|
53
58
|
|
|
54
59
|
send_request payload: payload
|
|
55
60
|
rescue StandardError => e
|
|
@@ -73,8 +78,8 @@ module OpenPanel
|
|
|
73
78
|
# @param property [String] property to increment
|
|
74
79
|
# @param value [Integer] value to increment by
|
|
75
80
|
def increment_property(user, property = 'visits', value = 1)
|
|
76
|
-
payload = { property: property, value: value }
|
|
77
|
-
payload = { type: TRACKING_TYPE_INCREMENT,
|
|
81
|
+
payload = { profileId: user.profile_id, property: property, value: value }
|
|
82
|
+
payload = { type: TRACKING_TYPE_INCREMENT, payload: payload }
|
|
78
83
|
|
|
79
84
|
send_request payload: payload
|
|
80
85
|
end
|
|
@@ -84,8 +89,8 @@ module OpenPanel
|
|
|
84
89
|
# @param property [String] property to increment
|
|
85
90
|
# @param value [Integer] value to decrement by
|
|
86
91
|
def decrement_property(user, property = 'visits', value = 1)
|
|
87
|
-
payload = { property: property, value: value }
|
|
88
|
-
payload = { type: TRACKING_TYPE_DECREMENT,
|
|
92
|
+
payload = { profileId: user.profile_id, property: property, value: value }
|
|
93
|
+
payload = { type: TRACKING_TYPE_DECREMENT, payload: payload }
|
|
89
94
|
|
|
90
95
|
send_request payload: payload
|
|
91
96
|
end
|
|
@@ -100,6 +105,35 @@ module OpenPanel
|
|
|
100
105
|
track 'revenue', profile_id: user.profile_id, payload: payload
|
|
101
106
|
end
|
|
102
107
|
|
|
108
|
+
# Create or update a group in OpenPanel
|
|
109
|
+
# @param group [OpenPanel::SDK::Group] group to create/update
|
|
110
|
+
def upsert_group(group)
|
|
111
|
+
payload = { id: group.id, type: group.type, name: group.name, properties: group.properties }
|
|
112
|
+
payload = { type: TRACKING_TYPE_GROUP, payload: payload }
|
|
113
|
+
|
|
114
|
+
send_request payload: payload
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Assign user to a group in OpenPanel
|
|
118
|
+
# @param profile_id [String] user profile ID
|
|
119
|
+
# @param group_id [String] group ID
|
|
120
|
+
def set_group(profile_id, group_id)
|
|
121
|
+
payload = { profileId: profile_id, groupIds: [group_id] }
|
|
122
|
+
payload = { type: TRACKING_TYPE_ASSIGN_GROUP, payload: payload }
|
|
123
|
+
|
|
124
|
+
send_request payload: payload
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Assign user to multiple groups in OpenPanel
|
|
128
|
+
# @param profile_id [String] user profile ID
|
|
129
|
+
# @param group_ids [Array<String>] list of group IDs
|
|
130
|
+
def set_groups(profile_id, group_ids)
|
|
131
|
+
payload = { profileId: profile_id, groupIds: group_ids }
|
|
132
|
+
payload = { type: TRACKING_TYPE_ASSIGN_GROUP, payload: payload }
|
|
133
|
+
|
|
134
|
+
send_request payload: payload
|
|
135
|
+
end
|
|
136
|
+
|
|
103
137
|
def fetch_device_id
|
|
104
138
|
return if @disabled
|
|
105
139
|
|
data/lib/openpanel/sdk.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: openpanel-sdk
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Thomas Stätter
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-05-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: faraday
|
|
@@ -47,10 +47,12 @@ extensions: []
|
|
|
47
47
|
extra_rdoc_files: []
|
|
48
48
|
files:
|
|
49
49
|
- ".env_sample"
|
|
50
|
+
- ".junie/AGENTS.md"
|
|
50
51
|
- LICENSE.txt
|
|
51
52
|
- README.md
|
|
52
53
|
- Rakefile
|
|
53
54
|
- lib/openpanel/sdk.rb
|
|
55
|
+
- lib/openpanel/sdk/group.rb
|
|
54
56
|
- lib/openpanel/sdk/identify_user.rb
|
|
55
57
|
- lib/openpanel/sdk/tracker.rb
|
|
56
58
|
- lib/openpanel/sdk/version.rb
|