-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,4 +173,4 @@ dist | |
.moon/cache | ||
.moon/docker | ||
|
||
lib | ||
/lib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as z from 'zod' | ||
import { articleScrollBack } from './tracking-schema/article-scroll-back' | ||
import { articleView } from './tracking-schema/article-view' | ||
import { page } from './tracking-schema/page' | ||
import { paywallTriggered } from './tracking-schema/paywall-triggered' | ||
|
||
// We must import 1 by 1 or we will lost the type info here | ||
export const trackEventSchema = z.discriminatedUnion('event', [articleScrollBack, articleView, page, paywallTriggered]) | ||
|
||
export type TrackEvent = z.infer<typeof trackEventSchema> | ||
|
||
export type ExtractProperties<EventName extends TrackEvent['event']> = Extract< | ||
TrackEvent, | ||
{ event: EventName } | ||
>['properties'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { z } from 'zod' | ||
|
||
export const articleScrollBack = defineTrackEvent({ | ||
event: 'article_scroll_back', | ||
properties: { | ||
clientId: z.string(), | ||
articleId: z.string().nullable(), | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { z } from 'zod' | ||
|
||
export const articleView = defineTrackEvent({ | ||
event: 'article_view', | ||
properties: { | ||
pathname: z.string(), | ||
isCustomTriggered: z.boolean(), | ||
clientId: z.string(), | ||
articleId: z.string().nullable(), | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { z } from 'zod' | ||
|
||
export const page = defineTrackEvent({ | ||
event: 'page', | ||
properties: { | ||
pathname: z.string(), | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from 'zod' | ||
|
||
export const paywallTriggered = defineTrackEvent({ | ||
event: 'paywall_triggered', | ||
properties: { | ||
isExceedFreeLimit: z.boolean(), | ||
clientId: z.string(), | ||
articleId: z.string().nullable(), | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { pushEvent } from '~/stores/track-events' | ||
import { ExtractProperties, TrackEvent, trackEventSchema } from './tracking-schema' | ||
|
||
export function sendTrack< | ||
EventName extends TrackEvent['event'], | ||
Properties extends ExtractProperties<EventName> = ExtractProperties<EventName>, | ||
>(event: EventName, properties: Properties) { | ||
try { | ||
const trackEvent = trackEventSchema.parse({ event, properties }) | ||
pushEvent(trackEvent) | ||
} catch {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { persistentMap } from '@nanostores/persistent' | ||
import { destr } from 'destr' | ||
import type { TrackEvent } from '../lib/tracking-schema' | ||
|
||
export interface BufferedEvent { | ||
/** | ||
* Event name | ||
*/ | ||
e: string | ||
|
||
/** | ||
* Properties | ||
*/ | ||
p: Record<string, any> | ||
|
||
/** | ||
* Timestamp | ||
*/ | ||
t: number | ||
} | ||
|
||
export const trackAtom = persistentMap( | ||
'storipress-paywall:', | ||
{ | ||
lastSynced: 0, | ||
records: [] as BufferedEvent[], | ||
token: '', | ||
}, | ||
{ | ||
encode: JSON.stringify, | ||
decode: destr, | ||
}, | ||
) | ||
|
||
export function pushEvent(record: TrackEvent) { | ||
trackAtom.set({ | ||
...trackAtom.get(), | ||
records: [...trackAtom.get().records, { e: record.event, p: record.properties ?? {}, t: Date.now() }], | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { z } from 'zod' | ||
|
||
interface DefineTrackEventInput<EventName extends string, Properties extends z.ZodRawShape> { | ||
event: EventName | ||
properties?: Properties | ||
} | ||
|
||
export function defineTrackEvent<EventName extends string, Properties extends z.ZodRawShape>( | ||
input: DefineTrackEventInput<EventName, Properties>, | ||
): z.ZodObject<{ | ||
event: z.ZodLiteral<EventName> | ||
properties: z.ZodObject<Properties> | z.ZodUndefined | ||
}> { | ||
return defineComplexTrackEvent({ | ||
event: input.event, | ||
properties: input.properties ? z.object(input.properties) : z.undefined(), | ||
}) | ||
} | ||
|
||
interface DefineComplexTrackEventInput<EventName extends string, Properties extends z.ZodTypeAny> { | ||
event: EventName | ||
properties: Properties | ||
} | ||
|
||
export function defineComplexTrackEvent<EventName extends string, Properties extends z.ZodTypeAny>( | ||
input: DefineComplexTrackEventInput<EventName, Properties>, | ||
): z.ZodObject<{ | ||
event: z.ZodLiteral<EventName> | ||
properties: Properties | ||
}> { | ||
return z.object({ | ||
event: z.literal(input.event), | ||
properties: input.properties, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters