analytics - identify user + extra actions added

This commit is contained in:
Michael Shanks 2020-09-29 15:26:56 +01:00
parent 1019078f79
commit 83261aeadf
17 changed files with 55 additions and 23 deletions

View File

@ -75,7 +75,7 @@
"fast-sort": "^2.2.0", "fast-sort": "^2.2.0",
"lodash": "^4.17.13", "lodash": "^4.17.13",
"mustache": "^4.0.1", "mustache": "^4.0.1",
"posthog-js": "1.3.1", "posthog-js": "1.4.5",
"shortid": "^2.2.15", "shortid": "^2.2.15",
"svelte-loading-spinners": "^0.1.1", "svelte-loading-spinners": "^0.1.1",
"svelte-portal": "^0.1.0", "svelte-portal": "^0.1.0",

View File

@ -158,6 +158,10 @@ export default {
find: "constants", find: "constants",
replacement: path.resolve(projectRootDir, "src/constants"), replacement: path.resolve(projectRootDir, "src/constants"),
}, },
{
find: "analytics",
replacement: path.resolve(projectRootDir, "src/analytics"),
},
], ],
customResolver, customResolver,
}), }),

View File

@ -2,15 +2,20 @@ import * as Sentry from "@sentry/browser"
import posthog from "posthog-js" import posthog from "posthog-js"
import api from "builderStore/api" import api from "builderStore/api"
const analyticsEnabled = process.env.NODE_ENV === "production"
function activate() { function activate() {
if (!analyticsEnabled) return
Sentry.init({ dsn: process.env.SENTRY_DSN }) Sentry.init({ dsn: process.env.SENTRY_DSN })
if (!process.env.POSTHOG_TOKEN) return if (!process.env.POSTHOG_TOKEN) return
posthog.init(process.env.POSTHOG_TOKEN, { posthog.init(process.env.POSTHOG_TOKEN, {
api_host: process.env.POSTHOG_URL, api_host: process.env.POSTHOG_URL,
}) })
posthog.set_config({ persistence: "cookie" })
} }
function identify(id) { function identify(id) {
if (!analyticsEnabled) return
if (!id) return if (!id) return
posthog.identify(id) posthog.identify(id)
Sentry.configureScope(scope => { Sentry.configureScope(scope => {
@ -19,6 +24,7 @@ function identify(id) {
} }
async function identifyByApiKey(apiKey) { async function identifyByApiKey(apiKey) {
if (!analyticsEnabled) return true
const response = await fetch( const response = await fetch(
`https://03gaine137.execute-api.eu-west-1.amazonaws.com/prod/account/id?api_key=${apiKey.trim()}` `https://03gaine137.execute-api.eu-west-1.amazonaws.com/prod/account/id?api_key=${apiKey.trim()}`
) )
@ -35,12 +41,14 @@ async function identifyByApiKey(apiKey) {
} }
function captureException(err) { function captureException(err) {
if (!analyticsEnabled) return
Sentry.captureException(err) Sentry.captureException(err)
} }
function captureEvent(event) { function captureEvent(eventName, props = {}) {
if (!process.env.POSTHOG_TOKEN) return if (!analyticsEnabled || !process.env.POSTHOG_TOKEN) return
posthog.capture(event) props.sourceApp = "builder"
posthog.capture(eventName, props)
} }
export default { export default {

View File

@ -1,7 +1,7 @@
import { getStore } from "./store" import { getStore } from "./store"
import { getBackendUiStore } from "./store/backend" import { getBackendUiStore } from "./store/backend"
import { getAutomationStore } from "./store/automation/" import { getAutomationStore } from "./store/automation/"
import analytics from "../analytics" import analytics from "analytics"
export const store = getStore() export const store = getStore()
export const backendUiStore = getBackendUiStore() export const backendUiStore = getBackendUiStore()
@ -9,9 +9,8 @@ export const automationStore = getAutomationStore()
export const initialise = async () => { export const initialise = async () => {
try { try {
if (process.env.NODE_ENV === "production") {
analytics.activate() analytics.activate()
} analytics.captureEvent("Builder Started")
} catch (err) { } catch (err) {
console.log(err) console.log(err)
} }

View File

@ -14,6 +14,7 @@ import { fetchComponentLibDefinitions } from "../loadComponentLibraries"
import { buildCodeForScreens } from "../buildCodeForScreens" import { buildCodeForScreens } from "../buildCodeForScreens"
import { generate_screen_css } from "../generate_css" import { generate_screen_css } from "../generate_css"
import { insertCodeMetadata } from "../insertCodeMetadata" import { insertCodeMetadata } from "../insertCodeMetadata"
import analytics from "analytics"
import { uuid } from "../uuid" import { uuid } from "../uuid"
import { import {
selectComponent as _selectComponent, selectComponent as _selectComponent,
@ -308,7 +309,9 @@ const addChildComponent = store => (componentToAdd, presetProps = {}) => {
state.currentView = "component" state.currentView = "component"
state.currentComponentInfo = newComponent.props state.currentComponentInfo = newComponent.props
analytics.captureEvent("Added Component", {
name: newComponent.props._component,
})
return state return state
}) })
} }

View File

@ -3,6 +3,7 @@
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import ActionButton from "components/common/ActionButton.svelte" import ActionButton from "components/common/ActionButton.svelte"
import { Input } from "@budibase/bbui" import { Input } from "@budibase/bbui"
import analytics from "analytics"
export let onClosed export let onClosed
@ -19,6 +20,7 @@
}) })
onClosed() onClosed()
notifier.success(`Automation ${name} created.`) notifier.success(`Automation ${name} created.`)
analytics.captureEvent("Automation Created", { name })
} }
</script> </script>

View File

@ -1,5 +1,6 @@
<script> <script>
import { automationStore } from "builderStore" import { automationStore } from "builderStore"
import analytics from "analytics"
export let blockDefinition export let blockDefinition
export let stepId export let stepId
@ -12,6 +13,9 @@
stepId, stepId,
type: blockType, type: blockType,
}) })
analytics.captureEvent("Added Automation Block", {
name: blockDefinition.name,
})
} }
</script> </script>

View File

@ -10,6 +10,7 @@
import { backendUiStore } from "builderStore" import { backendUiStore } from "builderStore"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import CreateEditRecord from "../modals/CreateEditRecord.svelte" import CreateEditRecord from "../modals/CreateEditRecord.svelte"
import analytics from "analytics"
const CALCULATIONS = [ const CALCULATIONS = [
{ {
@ -35,6 +36,7 @@
function saveView() { function saveView() {
backendUiStore.actions.views.save(view) backendUiStore.actions.views.save(view)
notifier.success(`View ${view.name} saved.`) notifier.success(`View ${view.name} saved.`)
analytics.captureEvent("Added View Calculate")
dropdown.hide() dropdown.hide()
} }
</script> </script>

View File

@ -10,6 +10,7 @@
import { backendUiStore } from "builderStore" import { backendUiStore } from "builderStore"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import CreateEditRecord from "../modals/CreateEditRecord.svelte" import CreateEditRecord from "../modals/CreateEditRecord.svelte"
import analytics from "analytics"
const CONDITIONS = [ const CONDITIONS = [
{ {
@ -63,6 +64,7 @@
backendUiStore.actions.views.save(view) backendUiStore.actions.views.save(view)
notifier.success(`View ${view.name} saved.`) notifier.success(`View ${view.name} saved.`)
dropdown.hide() dropdown.hide()
analytics.captureEvent("Added View Filter")
} }
function removeFilter(idx) { function removeFilter(idx) {

View File

@ -11,6 +11,7 @@
import { backendUiStore } from "builderStore" import { backendUiStore } from "builderStore"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import CreateEditRecord from "../modals/CreateEditRecord.svelte" import CreateEditRecord from "../modals/CreateEditRecord.svelte"
import analytics from "analytics"
let anchor let anchor
let dropdown let dropdown
@ -37,6 +38,7 @@
}) })
notifier.success(`View ${name} created`) notifier.success(`View ${name} created`)
dropdown.hide() dropdown.hide()
analytics.captureEvent("View Created", { name })
$goto(`../../../view/${name}`) $goto(`../../../view/${name}`)
} }
</script> </script>

View File

@ -3,6 +3,7 @@
import { backendUiStore } from "builderStore" import { backendUiStore } from "builderStore"
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import { DropdownMenu, Button, Icon, Input, Select } from "@budibase/bbui" import { DropdownMenu, Button, Icon, Input, Select } from "@budibase/bbui"
import analytics from "analytics"
export let table export let table
@ -19,6 +20,7 @@
$goto(`./model/${model._id}`) $goto(`./model/${model._id}`)
name = "" name = ""
dropdown.hide() dropdown.hide()
analytics.captureEvent("Table Created", { name })
} }
const onClosed = () => { const onClosed = () => {

View File

@ -3,7 +3,7 @@
import { store } from "builderStore" import { store } from "builderStore"
import api from "builderStore/api" import api from "builderStore/api"
import posthog from "posthog-js" import posthog from "posthog-js"
import analytics from "../../../analytics" import analytics from "analytics"
let keys = { budibase: "", sendGrid: "" } let keys = { budibase: "", sendGrid: "" }

View File

@ -14,7 +14,7 @@
import { getContext } from "svelte" import { getContext } from "svelte"
import { fade } from "svelte/transition" import { fade } from "svelte/transition"
import { post } from "builderStore/api" import { post } from "builderStore/api"
import analytics from "../../analytics" import analytics from "analytics"
const { open, close } = getContext("simple-modal") const { open, close } = getContext("simple-modal")
//Move this to context="module" once svelte-forms is updated so that it can bind to stores correctly //Move this to context="module" once svelte-forms is updated so that it can bind to stores correctly
@ -141,7 +141,7 @@
name: $createAppStore.values.applicationName, name: $createAppStore.values.applicationName,
}) })
const appJson = await appResp.json() const appJson = await appResp.json()
analytics.captureEvent("web_app_created", { analytics.captureEvent("App Created", {
name, name,
appId: appJson._id, appId: appJson._id,
}) })

View File

@ -4,7 +4,7 @@
import { notifier } from "builderStore/store/notifications" import { notifier } from "builderStore/store/notifications"
import api from "builderStore/api" import api from "builderStore/api"
import Spinner from "components/common/Spinner.svelte" import Spinner from "components/common/Spinner.svelte"
import analytics from "../../../analytics" import analytics from "analytics"
let deployed = false let deployed = false
let loading = false let loading = false
@ -26,10 +26,13 @@
notifier.success(`Your Deployment is Complete.`) notifier.success(`Your Deployment is Complete.`)
deployed = true deployed = true
loading = false loading = false
analytics.captureEvent("web_app_deployment", { analytics.captureEvent("Deployed App", {
appId, appId,
}) })
} catch (err) { } catch (err) {
analytics.captureEvent("Deploy App Failed", {
appId,
})
analytics.captureException(err) analytics.captureException(err)
notifier.danger("Deployment unsuccessful. Please try again later.") notifier.danger("Deployment unsuccessful. Please try again later.")
loading = false loading = false

View File

@ -9,7 +9,7 @@
import Spinner from "components/common/Spinner.svelte" import Spinner from "components/common/Spinner.svelte"
import CreateAppModal from "components/start/CreateAppModal.svelte" import CreateAppModal from "components/start/CreateAppModal.svelte"
import { Button } from "@budibase/bbui" import { Button } from "@budibase/bbui"
import analytics from "../analytics" import analytics from "analytics"
let promise = getApps() let promise = getApps()
@ -28,16 +28,15 @@
async function fetchKeys() { async function fetchKeys() {
const response = await api.get(`/api/keys/`) const response = await api.get(`/api/keys/`)
const res = await response.json() return await response.json()
return res.budibase
} }
async function checkIfKeysAndApps() { async function checkIfKeysAndApps() {
const key = await fetchKeys() const keys = await fetchKeys()
const apps = await getApps() const apps = await getApps()
if (key) { if (keys.userId) {
hasKey = true hasKey = true
analytics.identify(key.userId) analytics.identify(keys.userId)
} else { } else {
showCreateAppModal() showCreateAppModal()
} }

View File

@ -4847,9 +4847,10 @@ posix-character-classes@^0.1.0:
version "0.1.1" version "0.1.1"
resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab" resolved "https://registry.yarnpkg.com/posix-character-classes/-/posix-character-classes-0.1.1.tgz#01eac0fe3b5af71a2a6c02feabb8c1fef7e00eab"
posthog-js@1.3.1: posthog-js@1.4.5:
version "1.3.1" version "1.4.5"
resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.3.1.tgz#970acec1423eaa5dba0d2603410c9c70294e16da" resolved "https://registry.yarnpkg.com/posthog-js/-/posthog-js-1.4.5.tgz#b16235afe47938bd71eaed4ede3790c8b910ed71"
integrity sha512-Rzc5/DpuX55BqwNEbZB0tLav1gEinnr5H+82cbLiMtXLADlxmCwZiEaVXcC3XOqW0x8bcAEehicx1TbpfBamzA==
prelude-ls@~1.1.2: prelude-ls@~1.1.2:
version "1.1.2" version "1.1.2"

View File

@ -8,6 +8,7 @@ exports.fetch = async function(ctx) {
ctx.body = { ctx.body = {
budibase: process.env.BUDIBASE_API_KEY, budibase: process.env.BUDIBASE_API_KEY,
sendgrid: process.env.SENDGRID_API_KEY, sendgrid: process.env.SENDGRID_API_KEY,
userId: process.env.USERID_API_KEY,
} }
} }