-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathuseSourceTracking.ts
More file actions
200 lines (185 loc) · 5.16 KB
/
useSourceTracking.ts
File metadata and controls
200 lines (185 loc) · 5.16 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* Composable for source tracking analytics and tracking link management.
* Provides data for the source tracking dashboard and link CRUD operations.
*/
interface TrackingLink {
id: string
jobId: string | null
jobTitle: string | null
channel: string
name: string
code: string
utmSource: string | null
utmMedium: string | null
utmCampaign: string | null
utmTerm: string | null
utmContent: string | null
clickCount: number
applicationCount: number
isActive: boolean
createdAt: string
updatedAt: string
}
interface SourceStats {
channelBreakdown: { channel: string; count: number }[]
topLinks: {
id: string
name: string
channel: string
code: string
jobTitle: string | null
clickCount: number
applicationCount: number
isActive: boolean
}[]
funnel: Record<string, Record<string, number>>
dailyTrend: { date: string; channel: string; count: number }[]
recentAttributed: {
applicationId: string
channel: string
utmSource: string | null
utmCampaign: string | null
referrerDomain: string | null
trackingLinkName: string | null
candidateFirstName: string
candidateLastName: string
candidateEmail: string
jobTitle: string
status: string
appliedAt: string
}[]
topReferrerDomains: { domain: string | null; count: number }[]
summary: {
totalTracked: number
totalUntracked: number
attributionRate: number
}
}
export function useSourceTracking(options?: {
jobId?: Ref<string | undefined> | string
from?: Ref<string | undefined> | string
to?: Ref<string | undefined> | string
}) {
const jobId = computed(() => toValue(options?.jobId))
const from = computed(() => toValue(options?.from))
const to = computed(() => toValue(options?.to))
// ─── Source stats ─────────────────────────
const {
data: stats,
status: statsStatus,
error: statsError,
refresh: refreshStats,
} = useFetch<SourceStats>('/api/source-tracking/stats', {
key: 'source-stats',
headers: useRequestHeaders(['cookie']),
query: computed(() => {
const q: Record<string, string> = {}
if (jobId.value) q.jobId = jobId.value
if (from.value) q.from = from.value
if (to.value) q.to = to.value
return q
}),
})
const channelBreakdown = computed(() => stats.value?.channelBreakdown ?? [])
const topLinks = computed(() => stats.value?.topLinks ?? [])
const funnel = computed(() => stats.value?.funnel ?? {})
const dailyTrend = computed(() => stats.value?.dailyTrend ?? [])
const recentAttributed = computed(() => stats.value?.recentAttributed ?? [])
const topReferrerDomains = computed(() => stats.value?.topReferrerDomains ?? [])
const summary = computed(() => stats.value?.summary ?? { totalTracked: 0, totalUntracked: 0, attributionRate: 0 })
return {
channelBreakdown,
topLinks,
funnel,
dailyTrend,
recentAttributed,
topReferrerDomains,
summary,
statsStatus,
statsError,
refreshStats,
}
}
/**
* Composable for managing tracking links (CRUD operations).
*/
export function useTrackingLinks(options?: {
jobId?: Ref<string | undefined> | string
channel?: Ref<string | undefined> | string
}) {
const toast = useToast()
const jobId = computed(() => toValue(options?.jobId))
const channel = computed(() => toValue(options?.channel))
const {
data,
status: fetchStatus,
error,
refresh,
} = useFetch<{ data: TrackingLink[]; total: number }>('/api/tracking-links', {
key: 'tracking-links',
headers: useRequestHeaders(['cookie']),
query: computed(() => {
const q: Record<string, string> = {}
if (jobId.value) q.jobId = jobId.value
if (channel.value) q.channel = channel.value
return q
}),
})
const links = computed<TrackingLink[]>(() => data.value?.data ?? [])
const total = computed(() => data.value?.total ?? 0)
async function createLink(payload: {
jobId?: string
channel?: string
name: string
utmSource?: string
utmMedium?: string
utmCampaign?: string
utmTerm?: string
utmContent?: string
}) {
const created = await $fetch('/api/tracking-links', {
method: 'POST',
body: payload,
})
await refresh()
toast.success('Tracking link created')
return created
}
async function updateLink(id: string, payload: {
name?: string
channel?: string
utmSource?: string
utmMedium?: string
utmCampaign?: string
utmTerm?: string
utmContent?: string
isActive?: boolean
}) {
const updated = await $fetch(`/api/tracking-links/${id}`, {
method: 'PATCH',
body: payload,
})
await refresh()
return updated
}
async function deleteLink(id: string) {
await $fetch(`/api/tracking-links/${id}`, { method: 'DELETE' })
await refresh()
toast.success('Tracking link deleted')
}
async function toggleLink(id: string, isActive: boolean) {
await updateLink(id, { isActive })
toast.success(isActive ? 'Link activated' : 'Link deactivated')
}
return {
links,
total,
fetchStatus,
error,
refresh,
createLink,
updateLink,
deleteLink,
toggleLink,
}
}