From 53e45917e1f1dd1d26ada26c8751edde4f2e6b08 Mon Sep 17 00:00:00 2001 From: readme-bot Date: Sat, 5 Aug 2023 18:12:25 +0800 Subject: [PATCH] add ga4 --- package.json | 5 ++++ src-tauri/Cargo.lock | 1 - src-tauri/Cargo.toml | 1 - src-tauri/src/main.rs | 3 ++- src-tauri/src/migration.rs | 36 ++++++++++++++++++++--------- src/main.tsx | 38 +++++++++++++++++++++++++++++++ src/middlelayers/configuration.ts | 6 +++++ src/utils/app.ts | 10 ++++++++ yarn.lock | 5 ++++ 9 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 src/utils/app.ts diff --git a/package.json b/package.json index 00cf672..5dce227 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,10 @@ "private": true, "version": "0.0.0", "type": "module", + "repository": { + "type": "git", + "url": "https://github.com/domechn/track3.git" + }, "scripts": { "dev": "vite", "build": "tsc && vite build", @@ -22,6 +26,7 @@ "react": "^18.2.0", "react-chartjs-2": "^5.2.0", "react-dom": "^18.2.0", + "react-ga4": "^2.1.0", "react-hot-toast": "^2.4.1", "tauri-plugin-sql-api": "https://github.com/tauri-apps/tauri-plugin-sql", "uuid": "^9.0.0", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 95568bd..990b5b7 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4251,7 +4251,6 @@ dependencies = [ "tauri-plugin-sql", "tokio", "uuid 1.3.3", - "version-compare 0.1.1", ] [[package]] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 5a22c20..084a5a4 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -26,7 +26,6 @@ sqlx = {version = "0.6", features = ["runtime-tokio-rustls", "sqlite"] } tauri = {version = "1.2", features = ["app-all", "dialog-open", "dialog-save", "fs-read-file", "fs-write-file", "http-all", "process-relaunch", "updater"] } tokio = {version = "1", features = ["sync"] } uuid = "1.3.3" -version-compare = "0.1.1" [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index c06b266..643b1a7 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -162,6 +162,7 @@ async fn get_polybase_namespace(handle: tauri::AppHandle) -> Result bool { // check sqlite file exists @@ -26,7 +29,7 @@ fn get_sqlite_file_path(path: &Path) -> String { String::from(path.join("track3.db").to_str().unwrap()) } -pub fn init_sqlite_tables(app_dir: &Path, resource_dir: &Path) { +pub fn init_sqlite_tables(app_version:String, app_dir: &Path, resource_dir: &Path) { println!("start initing sqlite tables in rust"); let sqlite_path = get_sqlite_file_path(app_dir); let configuration = @@ -47,14 +50,25 @@ pub fn init_sqlite_tables(app_dir: &Path, resource_dir: &Path) { conn.execute(cloud_sync.as_str()).await.unwrap(); conn.execute(currency_rates_sync.as_str()).await.unwrap(); - sqlx::query("INSERT INTO configuration (id, data) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET data = ?") + // record current app version + sqlx::query(format!("INSERT INTO {} (id, data) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET data = ?", CONFIGURATION_TABLE_NAME).as_str()) .bind(VERSION_CONFIGURATION_ID) - .bind(CURRENT_VERSION) - .bind(CURRENT_VERSION) + .bind(app_version.as_str()) + .bind(app_version.as_str()) .execute(&mut conn) .await .unwrap(); + let client_id = Uuid::new_v4(); + // record client id + sqlx::query(format!("INSERT OR IGNORE INTO {} (id, data) VALUES (?, ?)", CONFIGURATION_TABLE_NAME).as_str()) + .bind(CLIENT_ID_CONFIGURATION_ID) + .bind(client_id.to_string()) + .execute(&mut conn) + .await + .unwrap(); + + conn.close().await.unwrap(); println!("init sqlite tables in tokio spawn done"); }); @@ -104,7 +118,7 @@ pub fn is_from_v02_to_v03(path: &Path) -> Result("select * from configuration where id = ?") + let data = sqlx::query_as::<_, Configuration>(format!("select * from {} where id = ?", CONFIGURATION_TABLE_NAME).as_str()) .bind(VERSION_CONFIGURATION_ID) .fetch_one(&mut conn) .await; @@ -112,7 +126,7 @@ pub fn is_from_v02_to_v03(path: &Path) -> Result Ok(compare_to(data.data ,"v0.3", Cmp::Lt) == Ok(true)), + Ok(data) => Ok(version::compare(&data.data, "v0.3").unwrap_or(-1) > 0), Err(e) => { // if error is RowNotFound, not need to migrate match e { @@ -157,7 +171,7 @@ pub fn migrate_from_v01_to_v02(app_dir: &Path, resource_dir: &Path) { // insert data_v2 to assets_v2 for row in data_v2 { - sqlx::query("insert into assets_v2 (uuid, symbol, amount, value, price, createdAt) values (?, ?, ?, ?, ?, ?)") + sqlx::query(format!("insert into {} (uuid, symbol, amount, value, price, createdAt) values (?, ?, ?, ?, ?, ?)", ASSETS_V2_TABLE_NAME).as_str()) .bind(row.uuid) .bind(row.symbol) .bind(row.amount) @@ -192,7 +206,7 @@ pub fn migrate_from_v02_to_v03(app_dir: &Path, resource_dir: &Path) { let mut conn = SqliteConnection::connect(&sqlite_path).await.unwrap(); conn.execute(assets_v2.as_str()).await.unwrap(); - sqlx::query("INSERT INTO configuration (id, data) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET data = ?") + sqlx::query(format!("INSERT INTO {} (id, data) VALUES (?, ?) ON CONFLICT(id) DO UPDATE SET data = ?", CONFIGURATION_TABLE_NAME).as_str()) .bind(VERSION_CONFIGURATION_ID) .bind(new_version) .bind(new_version) diff --git a/src/main.tsx b/src/main.tsx index 7d6d78e..579e5ea 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -2,9 +2,47 @@ import React from "react"; import ReactDOM from "react-dom/client"; import App from "./App"; import "./style.css"; +import ReactGA from "react-ga4"; +import { getClientID, getVersion } from './utils/app' + ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( ); + +function disableContextmenu() { + if (window.location.hostname !== 'tauri.localhost') { + return + } + document.addEventListener('contextmenu', e => { + e.preventDefault(); + return false; + }, { capture: true }) +} + +disableContextmenu() + +// ga4 +;(async () => { + const GAID = "G-QTHN28P1Q3" + try { + const cid = await getClientID() + const version = await getVersion() + ReactGA.initialize([{ + trackingId: GAID, + gaOptions: { + app_version: version, + clientId: cid + }, + gtagOptions: { + app_version: version, + clientId: cid + } + }]) + } catch (e) { + ReactGA.initialize(GAID) + throw e + } +})() diff --git a/src/middlelayers/configuration.ts b/src/middlelayers/configuration.ts index e421fa8..b65e7a7 100644 --- a/src/middlelayers/configuration.ts +++ b/src/middlelayers/configuration.ts @@ -8,6 +8,7 @@ import { getCurrencyRate, getDefaultCurrencyRate } from './currency' const prefix = "!ent:" const fixId = "1" const cloudSyncFixId = "2" +const clientInfoFixId = "998" export async function getConfiguration(): Promise { const model = await getConfigurationById(fixId) @@ -95,3 +96,8 @@ export async function getCurrentPreferCurrency(): Promise { return getCurrencyRate(pc) } + +export async function getClientIDConfiguration(): Promise { + const model = await getConfigurationById(clientInfoFixId) + return model?.data +} diff --git a/src/utils/app.ts b/src/utils/app.ts new file mode 100644 index 0000000..83e702c --- /dev/null +++ b/src/utils/app.ts @@ -0,0 +1,10 @@ +import * as api from '@tauri-apps/api' +import { getClientIDConfiguration } from '../middlelayers/configuration' + +export async function getVersion() { + return api.app.getVersion() +} + +export async function getClientID() { + return getClientIDConfiguration() +} diff --git a/yarn.lock b/yarn.lock index 88d233d..f75bf52 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1213,6 +1213,11 @@ react-dom@^18.2.0: loose-envify "^1.1.0" scheduler "^0.23.0" +react-ga4@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/react-ga4/-/react-ga4-2.1.0.tgz#56601f59d95c08466ebd6edfbf8dede55c4678f9" + integrity sha512-ZKS7PGNFqqMd3PJ6+C2Jtz/o1iU9ggiy8Y8nUeksgVuvNISbmrQtJiZNvC/TjDsqD0QlU5Wkgs7i+w9+OjHhhQ== + react-hot-toast@^2.4.1: version "2.4.1" resolved "https://registry.yarnpkg.com/react-hot-toast/-/react-hot-toast-2.4.1.tgz#df04295eda8a7b12c4f968e54a61c8d36f4c0994"