mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 00:58:32 +02:00
Track screen size, os, and version
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
use sqlx::types::JsonValue;
|
use sqlx::types::JsonValue;
|
||||||
|
use tauri::{async_runtime, AppHandle, Manager};
|
||||||
|
|
||||||
use crate::is_dev;
|
use crate::is_dev;
|
||||||
|
|
||||||
@@ -44,32 +45,74 @@ fn action_name(action: AnalyticsAction) -> &'static str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn track_event(
|
pub fn track_event(
|
||||||
|
app_handle: &AppHandle,
|
||||||
resource: AnalyticsResource,
|
resource: AnalyticsResource,
|
||||||
action: AnalyticsAction,
|
action: AnalyticsAction,
|
||||||
attributes: Option<JsonValue>,
|
attributes: Option<JsonValue>,
|
||||||
) {
|
) {
|
||||||
let event = format!("{}.{}", resource_name(resource), action_name(action));
|
async_runtime::block_on(async move {
|
||||||
let attributes_json = attributes.unwrap_or("{}".to_string().into()).to_string();
|
let event = format!("{}.{}", resource_name(resource), action_name(action));
|
||||||
let params = vec![
|
let attributes_json = attributes.unwrap_or("{}".to_string().into()).to_string();
|
||||||
("e", event.clone()),
|
let info = app_handle.package_info();
|
||||||
("a", attributes_json.clone()),
|
let params = vec![
|
||||||
("id", "site_zOK0d7jeBy2TLxFCnZ".to_string()),
|
("e", event.clone()),
|
||||||
];
|
("a", attributes_json.clone()),
|
||||||
let url = format!("https://t.yaak.app/t/e");
|
("id", "site_zOK0d7jeBy2TLxFCnZ".to_string()),
|
||||||
let req = reqwest::Client::builder()
|
("v", info.version.clone().to_string()),
|
||||||
.build()
|
("os", get_os().to_string()),
|
||||||
.unwrap()
|
("xy", get_window_size(app_handle)),
|
||||||
.get(&url)
|
];
|
||||||
.query(¶ms);
|
let url = format!("https://t.yaak.app/t/e");
|
||||||
|
let req = reqwest::Client::builder()
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
|
.get(&url)
|
||||||
|
.query(¶ms);
|
||||||
|
|
||||||
if is_dev() {
|
if is_dev() {
|
||||||
println!("Ignore dev analytics event: {}", event);
|
println!("Ignore dev analytics event: {} {:?}", event, params);
|
||||||
} else {
|
|
||||||
if let Err(e) = req.send().await {
|
|
||||||
println!("Error sending analytics event: {}", e);
|
|
||||||
} else {
|
} else {
|
||||||
println!("Sent analytics event: {}", event);
|
if let Err(e) = req.send().await {
|
||||||
|
println!("Error sending analytics event: {}", e);
|
||||||
|
} else {
|
||||||
|
println!("Sent analytics event: {}", event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_os() -> &'static str {
|
||||||
|
if cfg!(target_os = "windows") {
|
||||||
|
"windows"
|
||||||
|
} else if cfg!(target_os = "macos") {
|
||||||
|
"macos"
|
||||||
|
} else if cfg!(target_os = "linux") {
|
||||||
|
"linux"
|
||||||
|
} else {
|
||||||
|
"unknown"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_window_size(app_handle: &AppHandle) -> String {
|
||||||
|
let window = match app_handle.windows().into_values().next() {
|
||||||
|
Some(w) => w,
|
||||||
|
None => return "unknown".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let current_monitor = match window.current_monitor() {
|
||||||
|
Ok(Some(m)) => m,
|
||||||
|
_ => return "unknown".to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let scale_factor = current_monitor.scale_factor();
|
||||||
|
let size = current_monitor.size();
|
||||||
|
let width: f64 = size.width as f64 / scale_factor;
|
||||||
|
let height: f64 = size.height as f64 / scale_factor;
|
||||||
|
|
||||||
|
format!(
|
||||||
|
"{}x{}",
|
||||||
|
(width / 100.0).round() * 100.0,
|
||||||
|
(height / 100.0).round() * 100.0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ use tokio::sync::Mutex;
|
|||||||
|
|
||||||
use window_ext::TrafficLightWindowExt;
|
use window_ext::TrafficLightWindowExt;
|
||||||
|
|
||||||
use crate::analytics::{AnalyticsAction, AnalyticsResource};
|
use crate::analytics::{track_event, AnalyticsAction, AnalyticsResource};
|
||||||
|
|
||||||
mod analytics;
|
mod analytics;
|
||||||
mod models;
|
mod models;
|
||||||
@@ -817,10 +817,12 @@ fn main() {
|
|||||||
w.restore_state(StateFlags::all())
|
w.restore_state(StateFlags::all())
|
||||||
.expect("Failed to restore window state");
|
.expect("Failed to restore window state");
|
||||||
|
|
||||||
tauri::async_runtime::block_on(async move {
|
track_event(
|
||||||
analytics::track_event(AnalyticsResource::App, AnalyticsAction::Launch, None)
|
app_handle,
|
||||||
.await;
|
AnalyticsResource::App,
|
||||||
})
|
AnalyticsAction::Launch,
|
||||||
|
None,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExitRequested { api, .. } => {
|
// ExitRequested { api, .. } => {
|
||||||
|
|||||||
Reference in New Issue
Block a user