Switch to Plausible analytics

Detects automatically route changes

- use provided script
- specify domain
- send event name with optional props
This commit is contained in:
2022-10-25 16:43:51 +02:00
parent 5c4337a9a6
commit 25f62c0df0
8 changed files with 19 additions and 57 deletions

View File

@@ -1,37 +1,15 @@
<script lang="ts">
// @ts-nocheck
import { page } from '$app/stores'
import { sendPage } from '$utils/analytics'
export let appKey: any
export let url: any
export let enabled: boolean = process.env.NODE_ENV !== 'development'
let loaded = false
const handleLoad = () => {
// Init Countly
if (Countly) {
Countly.init({
app_key: appKey,
url,
})
Countly.track_sessions()
Countly.track_pageview()
}
loaded = true
}
// Send page to Analytics when changing path
$: enabled && $page.url.pathname && loaded && sendPage()
export let domain: string
export let enabled: boolean = !import.meta.env.DEV
</script>
<svelte:head>
{#if enabled}
<script defer src="https://cdn.jsdelivr.net/npm/countly-sdk-web@latest/lib/countly.min.js" on:load={handleLoad} />
<script defer data-domain={domain} src="https://analytics.flayks.com/js/{import.meta.env.DEV ? 'script.local' : 'plausible'}.js"></script>
<script>
window.plausible = window.plausible || function() {
(window.plausible.q = window.plausible.q || []).push(arguments)
}
</script>
{/if}
</svelte:head>
<noscript>
<img src="{url}/pixel.png?app_key={appKey}&begin_session=1" alt="countly" width="0" height="0" />
</noscript>

View File

@@ -31,7 +31,7 @@
// If successful
if (result.data.success) {
sendEvent({ action: 'newsletterSubscribe' })
sendEvent('newsletterSubscribe')
update()
} else {
// Hide message for errors

View File

@@ -23,7 +23,7 @@
isOpen = !isOpen
// Record opening event
!isOpen && sendEvent({ action: 'switcherOpen' })
!isOpen && sendEvent('switcherOpen')
}
/**

View File

@@ -54,7 +54,7 @@
class={'is-disabled'}
on:click={() => {
filterLocation(slug)
sendEvent({ action: 'filterContinent' })
sendEvent('filterContinent')
}}
>
<svg width="12" height="12">

View File

@@ -4,7 +4,7 @@
import { browser } from '$app/environment'
import { page } from '$app/stores'
import { beforeNavigate } from '$app/navigation'
import { PUBLIC_ANALYTICS_KEY, PUBLIC_ANALYTICS_URL } from '$env/static/public'
import { PUBLIC_ANALYTICS_DOMAIN } from '$env/static/public'
import type { PageData } from './$types'
import { onMount, setContext } from 'svelte'
import { pageLoading, previousPage } from '$utils/stores'
@@ -77,8 +77,5 @@
<SmoothScroll />
{#if browser}
<Analytics
appKey={PUBLIC_ANALYTICS_KEY}
url={PUBLIC_ANALYTICS_URL}
/>
<Analytics domain={PUBLIC_ANALYTICS_DOMAIN} />
{/if}

View File

@@ -319,7 +319,7 @@
<a href="#step-{index + 1}" class="title-big"
on:click|preventDefault={() => {
currentStep = index
sendEvent({ action: 'aboutStepSwitch' })
sendEvent('aboutStepSwitch')
}}
>
<span>{title}</span>

View File

@@ -1,21 +1,8 @@
// @ts-nocheck
// Send page
export const sendPage = (path: string = '') => {
if (typeof Countly !== 'undefined') {
Countly.track_pageview(path)
}
}
// Send event
export const sendEvent = ({ action, segments = {}, amount = 1 }) => {
if (typeof Countly !== 'undefined') {
Countly.add_event({
key: action,
count: amount,
segmentation: {
...segments
}
})
export const sendEvent = (action: string, props?: any) => {
if (typeof plausible !== 'undefined') {
plausible(action, props)
}
}

View File

@@ -154,7 +154,7 @@ export const mailtoClipboard = (node: HTMLElement) => {
}))
// Record event in analytics
sendEvent({ action: 'emailCopy' })
sendEvent('emailCopy')
event.preventDefault()
}