Browse Source

feat: posthog instrument module

Weihua Lu 4 years ago
parent
commit
87b65ec220

+ 1 - 0
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",

+ 0 - 4
resources/electron.html

@@ -53,9 +53,5 @@ const portal = new MagicPortal(worker);
 <script defer src="./js/code-editor.js"></script>
 <script defer src="./js/age-encryption.js"></script>
 <script defer src="./js/excalidraw.js"></script>
-<script>
-    !function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
-  posthog.init('qUumrWobEk2dKiKt1b32CMEZy8fgNS94rb_Bq4WutPA',{api_host:'https://app.posthog.com', persistence:'localStorage'})
-</script>
 </body>
 </html>

+ 4 - 0
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"]

+ 14 - 20
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

+ 20 - 24
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.idb :as idb]
+            [frontend.modules.instrumentation.posthog :as posthog]
             [frontend.modules.shortcut.core :as shortcut]
-            [frontend.handler.events :as events]
-            [frontend.fs.watcher-handler :as fs-watcher-handler]
-            [frontend.ui :as ui]
+            [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]
             [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.version :as version]
-            [frontend.components.page :as page]
-            [frontend.components.editor :as editor]))
+            [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!"))

+ 37 - 0
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)))

+ 12 - 0
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"