Track screen size, os, and version

This commit is contained in:
Gregory Schier
2023-11-08 09:49:29 -08:00
parent 98ed12a2df
commit c4926f430a
2 changed files with 70 additions and 25 deletions

View File

@@ -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(&params); let url = format!("https://t.yaak.app/t/e");
let req = reqwest::Client::builder()
.build()
.unwrap()
.get(&url)
.query(&params);
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
)
}

View File

@@ -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, .. } => {