Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bridge/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "relwave-bridge",
"version": "1.0.1",
"version": "1.0.2",
"type": "commonjs",
"main": "dist/index.cjs",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "relwave",
"private": true,
"version": "1.0.1",
"version": "1.0.2",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
25 changes: 17 additions & 8 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,25 @@ async fn main() {

builder
.setup(|app| {
let has_aptabase = option_env!("APTABASE_APP_KEY")
.map(String::from)
.or_else(|| std::env::var("APTABASE_APP_KEY").ok())
.is_some();
let analytics = AnalyticsService::new(app.handle());
let analytics_clone = analytics.clone();
let app_handle = app.handle().clone();
tauri::async_runtime::spawn(async move {
let is_analytics_enabled = analytics_clone.is_enabled().await;

if has_aptabase {
let _ = app.track_event("app_started", None);
}
let has_aptabase = option_env!("APTABASE_APP_KEY")
.map(String::from)
.or_else(|| std::env::var("APTABASE_APP_KEY").ok())
.is_some();

if has_aptabase && is_analytics_enabled {
let _ = app_handle.track_event("app_started", None);
println!("🚀 Analytics is ON: Successfully dispatched 'app_started' event.");
} else if has_aptabase && !is_analytics_enabled {
println!("🛑 Analytics is OFF: User opted out, no telemetry will be sent.");
}
});

let analytics = AnalyticsService::new(app.handle());
app.manage(analytics);

let handle = app.handle().clone();
Expand Down
33 changes: 28 additions & 5 deletions src-tauri/src/services/analytics.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
use std::sync::Arc;
use tauri::AppHandle;
use tauri::{AppHandle, Manager};
use tokio::sync::RwLock;
use std::fs;

#[derive(Clone)]
pub struct AnalyticsService {
enabled: Arc<RwLock<bool>>,
app_handle: AppHandle,
}

impl AnalyticsService {
pub fn new(_app: &AppHandle) -> Self {
pub fn new(app: &AppHandle) -> Self {
let mut enabled = false;
if let Ok(path) = app.path().app_data_dir() {
let config_path = path.join("analytics.json");
if let Ok(content) = fs::read_to_string(&config_path) {
if let Ok(val) = serde_json::from_str::<serde_json::Value>(&content) {
if let Some(e) = val.get("enabled").and_then(|v| v.as_bool()) {
enabled = e;
}
}
}
}

Self {
enabled: Arc::new(RwLock::new(false)),
enabled: Arc::new(RwLock::new(enabled)),
app_handle: app.clone(),
}
}

Expand All @@ -21,10 +36,18 @@ impl AnalyticsService {
pub async fn set_enabled(&self, enabled: bool) {
let mut e = self.enabled.write().await;
*e = enabled;

if let Ok(path) = self.app_handle.path().app_data_dir() {
let _ = fs::create_dir_all(&path);
let config_path = path.join("analytics.json");
let content = serde_json::json!({ "enabled": enabled }).to_string();
let _ = fs::write(&config_path, content);
}

if enabled {
println!("Analytics have been enabled by the user.");
println!("🚀 Analytics have been enabled by the user. Events will now be sent.");
} else {
println!("Analytics have been disabled by the user.");
println!("🛑 Analytics have been disabled by the user. Tracking stopped.");
}
}
}
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "RelWave",
"version": "1.0.1",
"version": "1.0.2",
"identifier": "tech.relwave.app",
"build": {
"beforeDevCommand": "vite",
Expand Down
Loading