File size: 861 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
export enum EventNames {
  OutboundLink = 'Outbound Link',
  DocLiked = 'Doc Liked',
  DocDisliked = 'Doc Disliked',
  LinkedToSandbox = 'Linked to Sandbox',
}

type EventFactory<
  TEventName extends EventNames,
  TAdditionalProps extends object = object,
> = {
  name: TEventName
  additionalProps?: TAdditionalProps
}

type OutboundLinkEvent = EventFactory<
  EventNames.OutboundLink,
  {
    linkTitle: string
  }
>

type VotingEvent = EventFactory<
  EventNames.DocLiked | EventNames.DocDisliked,
  {
    location: string
    title: string
  }
>

type SandboxEvent = EventFactory<
  EventNames.LinkedToSandbox,
  {
    title: string
  }
>

type Events = OutboundLinkEvent | VotingEvent | SandboxEvent

export const firePlausibleEvent = (event: Events) => {
  if (window.plausible) {
    window.plausible(event.name, { props: event.additionalProps })
  }
}