docs: add rust and ruby
This commit is contained in:
@@ -81,6 +81,16 @@ For most web projects, we recommend starting with one of these:
|
||||
- ✅ Async support
|
||||
- ✅ Django and Flask compatible
|
||||
|
||||
- **[Rust](/docs/sdks/rust)** - Rust SDK for server-side tracking
|
||||
- ✅ Async/await support
|
||||
- ✅ Type-safe API
|
||||
- ✅ Environment variable configuration
|
||||
|
||||
- **[Ruby](/docs/sdks/ruby)** - Ruby SDK for server-side tracking
|
||||
- ✅ Rails integration
|
||||
- ✅ Gem-based installation
|
||||
- ✅ Simple API
|
||||
|
||||
## Mobile SDKs
|
||||
|
||||
- **[React Native](/docs/sdks/react-native)** - Cross-platform mobile analytics
|
||||
@@ -108,7 +118,7 @@ Not sure which SDK to use? Here are some recommendations:
|
||||
- **Static website or HTML?** → Use [Script Tag](/docs/sdks/script)
|
||||
- **React app?** → Use [Web SDK](/docs/sdks/web) or [Script Tag](/docs/sdks/script)
|
||||
- **Next.js app?** → Use [Next.js SDK](/docs/sdks/nextjs)
|
||||
- **Server-side tracking?** → Use [Python](/docs/sdks/python) or [Node](/docs/sdks/javascript)
|
||||
- **Server-side tracking?** → Use [Python](/docs/sdks/python), [Rust](/docs/sdks/rust), [Ruby](/docs/sdks/ruby), or [Node](/docs/sdks/javascript)
|
||||
- **Mobile app?** → Use [React Native](/docs/sdks/react-native), [Swift](/docs/sdks/swift), or [Kotlin](/docs/sdks/kotlin)
|
||||
</Callout>
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@
|
||||
"remix",
|
||||
"express",
|
||||
"python",
|
||||
"rust",
|
||||
"ruby",
|
||||
"react-native",
|
||||
"swift",
|
||||
"kotlin",
|
||||
|
||||
218
apps/public/content/docs/(tracking)/sdks/ruby.mdx
Normal file
218
apps/public/content/docs/(tracking)/sdks/ruby.mdx
Normal file
@@ -0,0 +1,218 @@
|
||||
---
|
||||
title: Ruby
|
||||
---
|
||||
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps';
|
||||
import { Callout } from 'fumadocs-ui/components/callout';
|
||||
import CommonSdkConfig from '@/components/common-sdk-config.mdx';
|
||||
|
||||
The OpenPanel Ruby SDK allows you to track user behavior in your Ruby applications. This guide provides instructions for installing and using the Ruby SDK in your project.
|
||||
|
||||
<Callout>
|
||||
View the [Ruby SDK on GitHub](https://github.com/tstaetter/openpanel-ruby-sdk) for the latest updates and source code.
|
||||
</Callout>
|
||||
|
||||
## Installation
|
||||
|
||||
<Steps>
|
||||
### Install dependencies
|
||||
|
||||
If you're using Bundler, add to your `Gemfile`:
|
||||
|
||||
```bash
|
||||
bundle add openpanel-sdk
|
||||
```
|
||||
|
||||
Or install the gem directly:
|
||||
|
||||
```bash
|
||||
gem install openpanel-sdk
|
||||
```
|
||||
|
||||
### Set environment variables
|
||||
|
||||
Set your environment variables in a `.env` file:
|
||||
|
||||
```bash
|
||||
OPENPANEL_TRACK_URL=https://api.openpanel.dev/track
|
||||
OPENPANEL_CLIENT_ID=<YOUR_CLIENT_ID>
|
||||
OPENPANEL_CLIENT_SECRET=<YOUR_CLIENT_SECRET>
|
||||
```
|
||||
|
||||
### Initialize
|
||||
|
||||
Require and initialize the OpenPanel SDK:
|
||||
|
||||
```ruby
|
||||
require 'openpanel-sdk'
|
||||
|
||||
tracker = OpenPanel::SDK::Tracker.new
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
|
||||
<CommonSdkConfig />
|
||||
|
||||
Additional Ruby-specific options:
|
||||
|
||||
- `disabled` - Set to `true` to disable all event tracking
|
||||
- `env` - Environment name (e.g., `Rails.env.to_s`)
|
||||
|
||||
```ruby
|
||||
tracker = OpenPanel::SDK::Tracker.new(
|
||||
{ env: Rails.env.to_s },
|
||||
disabled: Rails.env.development?
|
||||
)
|
||||
```
|
||||
|
||||
</Steps>
|
||||
|
||||
## Usage
|
||||
|
||||
### Tracking Events
|
||||
|
||||
To track an event, use the `track` method:
|
||||
|
||||
```ruby
|
||||
tracker.track('test_event', payload: { name: 'test' })
|
||||
```
|
||||
|
||||
### Identifying Users
|
||||
|
||||
Create an `IdentifyUser` object and pass it to the `identify` method:
|
||||
|
||||
```ruby
|
||||
identify_user = OpenPanel::SDK::IdentifyUser.new
|
||||
identify_user.profile_id = 'user_123'
|
||||
identify_user.email = 'user@example.com'
|
||||
identify_user.first_name = 'John'
|
||||
identify_user.last_name = 'Doe'
|
||||
identify_user.properties = { tier: 'premium', company: 'Acme Inc' }
|
||||
|
||||
response = tracker.identify(identify_user)
|
||||
```
|
||||
|
||||
### Incrementing Properties
|
||||
|
||||
To increment a numeric property on a user profile:
|
||||
|
||||
```ruby
|
||||
tracker.increment_property(identify_user, 'visits', 1)
|
||||
```
|
||||
|
||||
### Decrementing Properties
|
||||
|
||||
To decrement a numeric property on a user profile:
|
||||
|
||||
```ruby
|
||||
tracker.decrement_property(identify_user, 'credits', 1)
|
||||
```
|
||||
|
||||
### Filtering Events
|
||||
|
||||
Filters are used to prevent sending events to OpenPanel in certain cases. You can filter events by passing a `filter` lambda to the `track` method:
|
||||
|
||||
```ruby
|
||||
filter = lambda { |payload|
|
||||
# Return true to send the event, false to skip it
|
||||
payload[:name] == 'test'
|
||||
}
|
||||
|
||||
response = tracker.track('test_event', payload: { name: 'test' }, filter: filter)
|
||||
# If filter returns false, response will be nil
|
||||
```
|
||||
|
||||
## Rails Integration
|
||||
|
||||
### Setting up the Tracker
|
||||
|
||||
Add the following to your `application_controller.rb`:
|
||||
|
||||
```ruby
|
||||
before_action :set_openpanel_tracker
|
||||
|
||||
protected
|
||||
|
||||
def set_openpanel_tracker
|
||||
@openpanel_tracker = OpenPanel::SDK::Tracker.new(
|
||||
{ env: Rails.env.to_s },
|
||||
disabled: Rails.env.development?
|
||||
)
|
||||
@openpanel_tracker.set_header 'x-client-ip', request.ip
|
||||
@openpanel_tracker.set_header 'user-agent', request.user_agent
|
||||
end
|
||||
```
|
||||
|
||||
### Tracking Events in Controllers
|
||||
|
||||
Use `@openpanel_tracker` in your controllers to track events:
|
||||
|
||||
```ruby
|
||||
def create
|
||||
@user = User.create(user_params)
|
||||
@openpanel_tracker.track('user_created', payload: { user_id: @user.id })
|
||||
redirect_to @user
|
||||
end
|
||||
```
|
||||
|
||||
### Identifying Users
|
||||
|
||||
Create a helper method to convert your app's user model to an `IdentifyUser`:
|
||||
|
||||
```ruby
|
||||
def identify_user_from_app_user(user, properties: {})
|
||||
iu = OpenPanel::SDK::IdentifyUser.new
|
||||
iu.profile_id = user.id.to_s
|
||||
iu.email = user.email
|
||||
iu.first_name = user.first_name
|
||||
iu.last_name = user.last_name
|
||||
iu.properties = properties
|
||||
iu
|
||||
end
|
||||
|
||||
# Usage in controller
|
||||
def show
|
||||
iu = identify_user_from_app_user(current_user)
|
||||
@openpanel_tracker.identify(iu)
|
||||
end
|
||||
```
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Setting Custom Headers
|
||||
|
||||
You can set custom headers for requests:
|
||||
|
||||
```ruby
|
||||
tracker.set_header 'x-client-ip', request.ip
|
||||
tracker.set_header 'user-agent', request.user_agent
|
||||
```
|
||||
|
||||
### Error Handling
|
||||
|
||||
The SDK returns a `Faraday::Response` object. Check the response status:
|
||||
|
||||
```ruby
|
||||
response = tracker.track('event', payload: { name: 'test' })
|
||||
if response&.status == 200
|
||||
puts 'Event tracked successfully'
|
||||
else
|
||||
puts "Failed to track event: #{response&.status}"
|
||||
end
|
||||
```
|
||||
|
||||
### Disabling Tracking
|
||||
|
||||
You can disable tracking during initialization or in specific environments:
|
||||
|
||||
```ruby
|
||||
# Disable during initialization
|
||||
tracker = OpenPanel::SDK::Tracker.new({}, disabled: true)
|
||||
|
||||
# Or disable in development
|
||||
tracker = OpenPanel::SDK::Tracker.new(
|
||||
{ env: Rails.env.to_s },
|
||||
disabled: Rails.env.development?
|
||||
)
|
||||
```
|
||||
|
||||
204
apps/public/content/docs/(tracking)/sdks/rust.mdx
Normal file
204
apps/public/content/docs/(tracking)/sdks/rust.mdx
Normal file
@@ -0,0 +1,204 @@
|
||||
---
|
||||
title: Rust
|
||||
---
|
||||
|
||||
import { Step, Steps } from 'fumadocs-ui/components/steps';
|
||||
import { Callout } from 'fumadocs-ui/components/callout';
|
||||
import CommonSdkConfig from '@/components/common-sdk-config.mdx';
|
||||
|
||||
The OpenPanel Rust SDK allows you to track user behavior in your Rust applications. This guide provides instructions for installing and using the Rust SDK in your project.
|
||||
|
||||
<Callout>
|
||||
View the [Rust SDK on GitHub](https://github.com/tstaetter/openpanel-rust-sdk/) for the latest updates and source code.
|
||||
</Callout>
|
||||
|
||||
## Installation
|
||||
|
||||
<Steps>
|
||||
### Install dependencies
|
||||
|
||||
Add the following to your `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
[dependencies]
|
||||
openpanel-sdk = "0.1.0"
|
||||
```
|
||||
|
||||
Or install via cargo:
|
||||
|
||||
```bash
|
||||
cargo add openpanel-sdk
|
||||
```
|
||||
|
||||
### Set environment variables
|
||||
|
||||
Set your environment variables in a `.env` file:
|
||||
|
||||
```bash
|
||||
OPENPANEL_TRACK_URL=https://api.openpanel.dev/track
|
||||
OPENPANEL_CLIENT_ID=<YOUR_CLIENT_ID>
|
||||
OPENPANEL_CLIENT_SECRET=<YOUR_CLIENT_SECRET>
|
||||
```
|
||||
|
||||
### Initialize
|
||||
|
||||
Import and initialize the OpenPanel SDK:
|
||||
|
||||
```rust
|
||||
use openpanel_sdk::sdk::Tracker;
|
||||
|
||||
let tracker = Tracker::try_new_from_env()?.with_default_headers()?;
|
||||
```
|
||||
|
||||
### Configuration Options
|
||||
|
||||
<CommonSdkConfig />
|
||||
|
||||
</Steps>
|
||||
|
||||
## Usage
|
||||
|
||||
### Tracking Events
|
||||
|
||||
To track an event, use the `track` method:
|
||||
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
use openpanel_sdk::sdk::Tracker;
|
||||
|
||||
let mut properties = HashMap::new();
|
||||
properties.insert("name".to_string(), "rust".to_string());
|
||||
|
||||
let response = tracker
|
||||
.track("test_event".to_string(), Some(properties), None)
|
||||
.await?;
|
||||
```
|
||||
|
||||
### Identifying Users
|
||||
|
||||
To identify a user, you need to convert your user struct into `user::IdentifyUser` by implementing the `From` trait:
|
||||
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
use openpanel_sdk::sdk::Tracker;
|
||||
use openpanel_sdk::user;
|
||||
|
||||
struct Address {
|
||||
pub street: String,
|
||||
pub city: String,
|
||||
pub zip: String,
|
||||
}
|
||||
|
||||
struct AppUser {
|
||||
pub id: String,
|
||||
pub email: String,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub address: Address,
|
||||
}
|
||||
|
||||
impl From<Address> for HashMap<String, String> {
|
||||
fn from(address: Address) -> Self {
|
||||
let mut properties = HashMap::new();
|
||||
properties.insert("street".to_string(), address.street);
|
||||
properties.insert("city".to_string(), address.city);
|
||||
properties.insert("zip".to_string(), address.zip);
|
||||
properties
|
||||
}
|
||||
}
|
||||
|
||||
impl From<AppUser> for user::IdentifyUser {
|
||||
fn from(app_user: AppUser) -> Self {
|
||||
Self {
|
||||
profile_id: app_user.id,
|
||||
email: app_user.email,
|
||||
first_name: app_user.first_name,
|
||||
last_name: app_user.last_name,
|
||||
properties: app_user.address.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Usage
|
||||
let user = AppUser { /* ... */ };
|
||||
let response = tracker.identify(user.into()).await?;
|
||||
```
|
||||
|
||||
### Incrementing Properties
|
||||
|
||||
To increment a numeric property on a user profile:
|
||||
|
||||
```rust
|
||||
let response = tracker
|
||||
.increment_property(profile_id, "visits", 1)
|
||||
.await?;
|
||||
```
|
||||
|
||||
### Decrementing Properties
|
||||
|
||||
To decrement a numeric property on a user profile:
|
||||
|
||||
```rust
|
||||
let response = tracker
|
||||
.decrement_property(profile_id, "credits", 1)
|
||||
.await?;
|
||||
```
|
||||
|
||||
### Filtering Events
|
||||
|
||||
Filters are used to prevent sending events to OpenPanel in certain cases. You can filter events by passing a `filter` function to the `track` method:
|
||||
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
|
||||
let filter = |properties: HashMap<String, String>| {
|
||||
// Return true to send the event, false to skip it
|
||||
properties.contains_key("required_key")
|
||||
};
|
||||
|
||||
let mut properties = HashMap::new();
|
||||
properties.insert("name".to_string(), "rust".to_string());
|
||||
|
||||
let response = tracker
|
||||
.track("test_event".to_string(), Some(properties), Some(&filter))
|
||||
.await;
|
||||
|
||||
// If filter returns false, the event won't be sent and an Err is returned
|
||||
match response {
|
||||
Ok(_) => println!("Event sent successfully"),
|
||||
Err(_) => println!("Event was filtered out"),
|
||||
}
|
||||
```
|
||||
|
||||
## Advanced Usage
|
||||
|
||||
### Error Handling
|
||||
|
||||
The SDK uses Rust's `Result` type for error handling. Always handle errors appropriately:
|
||||
|
||||
```rust
|
||||
match tracker.track("event".to_string(), Some(properties), None).await {
|
||||
Ok(response) => {
|
||||
if response.status() == 200 {
|
||||
println!("Event tracked successfully");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to track event: {}", e);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Async Runtime
|
||||
|
||||
The SDK uses async/await. Make sure you're running within an async runtime (e.g., Tokio):
|
||||
|
||||
```rust
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let tracker = Tracker::try_new_from_env()?.with_default_headers()?;
|
||||
// ... use tracker
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
@@ -11,6 +11,8 @@ import { RemixIcon } from './icons/remix-icon';
|
||||
import { RestIcon } from './icons/rest-icon';
|
||||
import { SwiftIcon } from './icons/swift-icon';
|
||||
import { VueIcon } from './icons/vue-icon';
|
||||
import { RustIcon } from './icons/rust-icon';
|
||||
import { RubyIcon } from './icons/ruby-icon';
|
||||
|
||||
export type Framework = {
|
||||
key: string;
|
||||
@@ -98,6 +100,20 @@ export const frameworks: Framework[] = [
|
||||
href: 'https://github.com/tbleckert/openpanel-laravel/tree/main',
|
||||
type: ['backend'],
|
||||
},
|
||||
{
|
||||
key: 'rust',
|
||||
IconComponent: RustIcon,
|
||||
name: 'Rust',
|
||||
href: 'https://openpanel.dev/docs/sdks/rust',
|
||||
type: ['backend'],
|
||||
},
|
||||
{
|
||||
key: 'ruby',
|
||||
IconComponent: RubyIcon,
|
||||
name: 'Ruby',
|
||||
href: 'https://openpanel.dev/docs/sdks/ruby',
|
||||
type: ['backend'],
|
||||
},
|
||||
{
|
||||
key: 'ios',
|
||||
IconComponent: SwiftIcon,
|
||||
|
||||
46
packages/sdks/_info/icons/ruby-icon.tsx
Normal file
46
packages/sdks/_info/icons/ruby-icon.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { IconProps } from './types';
|
||||
|
||||
export function RubyIcon({ className }: IconProps) {
|
||||
return (
|
||||
<svg
|
||||
fill="#000000"
|
||||
version="1.1"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
viewBox="0 0 512 512"
|
||||
className={className}
|
||||
>
|
||||
<g id="5151e0c8492e5103c096af88a51e4c6f">
|
||||
<path
|
||||
fill="currentColor"
|
||||
display="inline"
|
||||
d="M365.281,386.936l108.635,96.416l-213.754-16.794c41.878-25.3,76.426-55.513,102.294-79.567
|
||||
c0.426,0.083,0.879,0.129,1.332,0.129C364.283,387.119,364.782,387.06,365.281,386.936z M485.518,444.55l20.008-262.045
|
||||
c-9.898,25.284-25.557,54.73-45.857,86.172L485.518,444.55z M372.547,380.983l108.384,96.196l-28.936-196.865
|
||||
C428.142,315.82,399.5,352.122,372.547,380.983z M245.951,474.776c-23.345,12.934-47.373,23.235-71.707,30.757l249.786-16.798
|
||||
l-177.998-13.982L245.951,474.776z M37.347,348.551l65.429,152.89l53.851-178.4L37.347,348.551z M172.514,313.988l182.182,58.517
|
||||
l-46.488-184.204L172.514,313.988z M318.403,170.006l169.829-11.368L355.451,50.19L318.403,170.006z M415.002,2.919L303.56,2.415
|
||||
l49.543,34.69L415.002,2.919z M4.785,283.758L1.118,392.411l26.729-48.75L4.785,283.758z M65.668,327.408
|
||||
c21.184,1.351,42.583-3.343,64.17-12.792c14.541-6.891,29.626-15.612,44.767-25.863c21.715-15.342,43.6-34.532,65.645-56.38
|
||||
c79.292-78.583,111.895-166.467,77.246-201.459c-8.268-8.344-20.104-12.609-35.188-12.675
|
||||
c-45.66-0.197-113.905,34.175-172.069,91.842c-55.586,55.098-91.631,120.496-91.824,166.618
|
||||
c-0.068,16.399,4.469,29.319,13.483,38.407C39.915,323.205,51.306,326.487,65.668,327.408z M231.103,472.18
|
||||
c2.486-1.286,4.963-2.586,7.408-3.909l-73.322-141.472l-55.183,182.786c0.462-0.06,4.189-0.508,4.189-0.508
|
||||
c4.816-0.613,9.573-1.318,14.271-2.115c30.318-5.169,60.315-14.633,89.154-28.139C222.184,476.689,226.671,474.47,231.103,472.18z
|
||||
M380.843,355.491l1.739-1.996c1.214-1.424,2.437-2.875,3.654-4.331c0,0,3.768-4.486,5.232-6.268
|
||||
c2.464-2.98,4.913-6.021,7.371-9.079c2.258-2.82,4.51-5.649,6.758-8.529c2.358-3.021,4.716-6.048,7.056-9.134
|
||||
c5.297-6.973,10.653-14.284,16.377-22.36c6.057-8.544,11.908-17.142,17.416-25.584l-9.931-6.073
|
||||
c-26.371-16.125-79.37-48.606-114.203-70.003l45.074,178.595C371.585,366.118,376.018,361.104,380.843,355.491z M508.235,67.95
|
||||
l-140.23-19.689l133.688,109.327c4.697-15.496,7.756-30.524,9.088-44.699c0.192-1.987,0.315-3.985,0.435-5.998
|
||||
c0.114-1.852,0.179-3.711,0.229-5.574C511.766,89.244,510.685,78.029,508.235,67.95z M454.325,259.742
|
||||
c0.344-0.453,0.729-0.962,1.149-1.573c2.669-3.779,6.465-9.962,10.686-17.409c11.748-20.717,26.239-50.254,32.282-71.993
|
||||
L324.03,180.403c36.379,22.356,89.521,54.924,115.942,71.084l14.087,8.598C454.143,259.98,454.229,259.865,454.325,259.742z
|
||||
M245.264,466.128l0.545-0.307c38.046-21.084,72.05-47.021,110.142-84.087l-185.419-59.807L245.264,466.128z M0.5,411.942
|
||||
c0.819,24.146,5.943,44.081,15.241,59.391l8.658-103.113L0.5,411.942z M34.284,361.407l-9.99,119
|
||||
c14.234,18.62,33.252,26.935,61.528,26.935c2.848,0,5.806-0.078,8.978-0.233l-4.995-12.412
|
||||
C78.996,467.845,48.152,391.248,34.284,361.407z M506.144,60.604c-2.056-6.289-4.68-12.096-7.811-17.306
|
||||
c-12.632-21.015-34.452-34.461-64.871-39.976l-67.961,37.531L506.144,60.604z"
|
||||
/>
|
||||
</g>
|
||||
</svg>
|
||||
);
|
||||
}
|
||||
17
packages/sdks/_info/icons/rust-icon.tsx
Normal file
17
packages/sdks/_info/icons/rust-icon.tsx
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user