diff --git a/package.json b/package.json index 0107c4411..2603e6126 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "jszip": "^3.5.0", "mldoc": "0.6.18", "path": "^0.12.7", + "posthog-js": "^1.10.2", "react": "^17.0.2", "react-dom": "^17.0.2", "react-resize-context": "^3.0.0", diff --git a/resources/electron.html b/resources/electron.html index 56df49fcf..fa9605fad 100644 --- a/resources/electron.html +++ b/resources/electron.html @@ -53,9 +53,5 @@ const portal = new MagicPortal(worker); - diff --git a/shadow-cljs.edn b/shadow-cljs.edn index 981229499..afb46e04c 100644 --- a/shadow-cljs.edn +++ b/shadow-cljs.edn @@ -22,6 +22,10 @@ :release {:asset-path "https://asset.logseq.com/static/js"} :compiler-options {:infer-externs :auto :output-feature-set :es-next + ;; this cross-chunk-method-motion is needed + ;; idue to ssue related with + ;; https://github.com/thheller/shadow-cljs/issues/611 + :cross-chunk-method-motion false :source-map true :externs ["datascript/externs.js" "externs.js"] diff --git a/src/main/frontend/components/settings.cljs b/src/main/frontend/components/settings.cljs index f6ba50df4..b3170bae7 100644 --- a/src/main/frontend/components/settings.cljs +++ b/src/main/frontend/components/settings.cljs @@ -1,25 +1,25 @@ (ns frontend.components.settings - (:require [rum.core :as rum] - [frontend.ui :as ui] + (:require [clojure.string :as string] [frontend.components.svg :as svg] - [frontend.handler.notification :as notification] - [frontend.handler.user :as user-handler] - [frontend.handler.ui :as ui-handler] - [frontend.handler.repo :as repo-handler] + [frontend.config :as config] + [frontend.context.i18n :as i18n] + [frontend.date :as date] + [frontend.dicts :as dicts] + [frontend.handler :as handler] [frontend.handler.config :as config-handler] + [frontend.handler.notification :as notification] [frontend.handler.page :as page-handler] [frontend.handler.route :as route-handler] - [frontend.handler :as handler] + [frontend.handler.ui :as ui-handler] + [frontend.handler.user :as user-handler] + [frontend.modules.instrumentation.posthog :as posthog] [frontend.state :as state] - [frontend.version :refer [version]] + [frontend.ui :as ui] [frontend.util :as util] - [frontend.config :as config] - [frontend.dicts :as dicts] - [clojure.string :as string] + [frontend.version :refer [version]] [goog.object :as gobj] - [frontend.context.i18n :as i18n] [reitit.frontend.easy :as rfe] - [frontend.date :as date])) + [rum.core :as rum])) (rum/defcs set-email < (rum/local "" ::email) [state] @@ -145,15 +145,9 @@ :width 500 :height 500}]]) -(defn monitoring-opted-out - [] - (.. js/window -posthog has_opted_out_capturing)) - (defn set-sentry-disabled! [value] - (if value - (.. js/window -posthog opt_out_capturing) - (.. js/window -posthog opt_in_capturing)) + (posthog/opt-out value) (state/set-sentry-disabled! value)) (rum/defcs settings < rum/reactive diff --git a/src/main/frontend/handler.cljs b/src/main/frontend/handler.cljs index 41d495335..4c90adfbd 100644 --- a/src/main/frontend/handler.cljs +++ b/src/main/frontend/handler.cljs @@ -1,36 +1,31 @@ (ns frontend.handler - (:require [frontend.state :as state] + (:require [cljs-bean.core :as bean] + [electron.ipc :as ipc] + [electron.listener :as el] + [frontend.components.editor :as editor] + [frontend.components.page :as page] + [frontend.config :as config] [frontend.db :as db] [frontend.db-schema :as db-schema] - [frontend.util :as util :refer-macros [profile]] - [frontend.config :as config] - [frontend.storage :as storage] - [clojure.string :as string] - [promesa.core :as p] - [cljs-bean.core :as bean] - [frontend.date :as date] - [frontend.search :as search] - [frontend.search.db :as search-db] + [frontend.handler.common :as common-handler] + [frontend.handler.events :as events] + [frontend.handler.file :as file-handler] [frontend.handler.notification :as notification] [frontend.handler.page :as page-handler] [frontend.handler.repo :as repo-handler] - [frontend.handler.file :as file-handler] - [frontend.handler.editor :as editor-handler] [frontend.handler.ui :as ui-handler] - [frontend.handler.web.nfs :as nfs] - [frontend.modules.shortcut.core :as shortcut] - [frontend.handler.events :as events] - [frontend.fs.watcher-handler :as fs-watcher-handler] - [frontend.ui :as ui] - [goog.object :as gobj] [frontend.idb :as idb] - [lambdaisland.glogi :as log] - [frontend.handler.common :as common-handler] - [electron.listener :as el] - [electron.ipc :as ipc] + [frontend.modules.instrumentation.posthog :as posthog] + [frontend.modules.shortcut.core :as shortcut] + [frontend.search :as search] + [frontend.search.db :as search-db] + [frontend.state :as state] + [frontend.storage :as storage] + [frontend.util :as util] [frontend.version :as version] - [frontend.components.page :as page] - [frontend.components.editor :as editor])) + [goog.object :as gobj] + [lambdaisland.glogi :as log] + [promesa.core :as p])) (defn set-global-error-notification! [] @@ -203,7 +198,8 @@ (file-handler/run-writes-chan!) (shortcut/install-shortcuts!) (when (util/electron?) - (el/listen!)))) + (el/listen!)) + (posthog/init))) (defn stop! [] (prn "stop!")) diff --git a/src/main/frontend/modules/instrumentation/posthog.cljs b/src/main/frontend/modules/instrumentation/posthog.cljs new file mode 100644 index 000000000..03f0b8634 --- /dev/null +++ b/src/main/frontend/modules/instrumentation/posthog.cljs @@ -0,0 +1,37 @@ +(ns frontend.modules.instrumentation.posthog + (:require [frontend.config :as cfg] + [frontend.util :as util] + [frontend.version :refer [version]] + ["posthog-js" :as posthog])) + +(def ^:const token "qUumrWobEk2dKiKt1b32CMEZy8fgNS94rb_Bq4WutPA") +(def ^:const masked "masked") + +(defn register [] + (posthog/register + (clj->js + (cond-> + {:app_type (if (util/electron?) "electron" "web") + :app_env (if cfg/dev? "development" "production") + :app_ver version + :schema_ver 0 + ;; hack, did not find ways to hack data on-the-fly with posthog-js + :$ip masked + :$current_url masked})))) + +(def config + {:api_host "https://app.posthog.com" + :persistence "localStorage" + :mask_all_text true + :mask_all_element_attributes true + :loaded (fn [_] (register))}) + +(defn init [] + (posthog/init token (clj->js config)) + (when cfg/dev? + (posthog/debug))) + +(defn opt-out [opt-out?] + (if opt-out? + (posthog/opt_out_capturing) + (posthog/opt_in_capturing))) diff --git a/yarn.lock b/yarn.lock index 114a326f3..488844187 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2203,6 +2203,11 @@ fd-slicer@~1.1.0: dependencies: pend "~1.2.0" +fflate@^0.4.1: + version "0.4.8" + resolved "https://registry.yarnpkg.com/fflate/-/fflate-0.4.8.tgz#f90b82aefbd8ac174213abb338bd7ef848f0f5ae" + integrity sha512-FJqqoDBR00Mdj9ppamLa/Y7vxm+PRmNWA67N846RvsoYVMKB4q3y/de5PA7gUmRMYK/8CMz2GDZQmCRN1wBcWA== + file-entry-cache@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/file-entry-cache/-/file-entry-cache-6.0.0.tgz" @@ -4939,6 +4944,13 @@ postcss@^7.0.0, postcss@^7.0.1, postcss@^7.0.14, postcss@^7.0.2, postcss@^7.0.21 source-map "^0.6.1" supports-color "^6.1.0" +posthog-js@^1.10.2: + version "1.10.2" + resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.10.2.tgz#74d6c84f9675b65dfd4ff6f4051ed8d3cb974076" + integrity sha512-JNjWstHEexhj5CEKldSeYNyPJbtOvZQ3ZPL55fxU7+f+gTBL8RlOb8eFohCPYIk0VhMf2UM1rXxwVBOeMQQQFw== + dependencies: + fflate "^0.4.1" + prepend-http@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz"