-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathanalytics.ts
More file actions
102 lines (95 loc) · 2.97 KB
/
analytics.ts
File metadata and controls
102 lines (95 loc) · 2.97 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
import { scriptureConfig } from '$assets/config';
import { analytics, type AudioPlayer } from '$lib/data/stores';
import type { HistoryItem } from './history';
export function getBook(item: { collection?: string; book: string }) {
return scriptureConfig.bookCollections
?.find((x) => x.id === item.collection)
?.books.find((x) => x.id === item.book);
}
function getDamId(item: { book: any; chapter: string }) {
let damId;
if (item.book.audio.length > 0) {
const audio = item.book.audio.find((x) => x.num === Number(item.chapter));
const source = audio.src;
if (source) {
if (scriptureConfig.audio?.sources[source]?.type === 'fcbh') {
damId = scriptureConfig.audio.sources[source].damId;
}
}
}
return damId;
}
export function logScreenView(item: HistoryItem) {
const book = getBook({ ...item });
const chapter = item.chapter;
const bookAbbrev = book.abbreviation;
const damId = getDamId({ book, chapter });
const params = {
screenName: item.book,
bookCol: item.collection,
bookId: item.book,
bookAbbrev,
chapter,
damId
};
analytics.log('ab_screen_view', params);
}
export function logAudioPlay(audioPlayer: AudioPlayer) {
const bookCol = audioPlayer.collection;
const book = getBook({ collection: bookCol, book: audioPlayer.book });
const screenName = book.id;
const chapter = audioPlayer.chapter;
const bookAbbrev = book.abbreviation;
const damId = getDamId({ book, chapter });
const playing = audioPlayer.playing;
const params = {
screenName,
bookCol,
bookAbbrev,
chapter,
damId,
playing
};
analytics.log('ab_audio_play', params);
}
export function logAudioDuration(audioPlayer: AudioPlayer) {
const bookCol = audioPlayer.collection;
const book = getBook({ collection: bookCol, book: audioPlayer.book });
const screenName = book.id;
const chapter = audioPlayer.chapter;
const bookAbbrev = book.abbreviation;
const damId = getDamId({ book, chapter });
const playStart = audioPlayer.playStart;
const playEnd = Date.now();
const playDuration = (playEnd - playStart + 500) / 1000;
const params = {
screenName,
bookCol,
bookAbbrev,
chapter,
damId,
playDuration,
playStart,
playEnd
};
analytics.log('ab_audio_duration', params);
}
// TODO: Complete once shareApp is implemented
export function logShareApp(appName, appVersion, appType) {
analytics.log('ab_share_app', { appName, appVersion, appType });
}
export function logShareContent(
contentType: string,
bookCol: string,
bookAbbrev: string,
reference: string
) {
// include reference info (bookCol, bookAbbrev, verses selected)
const params = {
contentType,
bookCol,
bookAbbrev,
reference
};
analytics.log('ab_share_content', params);
}