-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookieManagement.service.ts
175 lines (159 loc) · 4.03 KB
/
cookieManagement.service.ts
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
import { COOKIE_TRACK_EVENT } from '../../constants/track-event.constant'
import { Tracker } from '../../interfaces/tracker'
import { push } from '../paqService/paq.service'
/**
* Disables all first party cookies. Existing cookies will be deleted in the next page view
*/
export function disableCookies() {
push([COOKIE_TRACK_EVENT.DISABLE_COOKIES])
}
/**
* Enables all first party cookies. Cookies will be created on the next tracking request
*/
export function enableCookies() {
push([COOKIE_TRACK_EVENT.ENABLE_COOKIES])
}
/**
* Deletes existing tracking cookies on the next page view
*/
export function deleteCookies() {
push([COOKIE_TRACK_EVENT.DELETE_COOKIES])
}
/**
* Returns true if cookies are enabled in this browser
*/
export function hasCookies(): Promise<boolean> {
return new Promise((resolve, reject) => {
try {
push([
function (this: Tracker) {
resolve(this.hasCookies())
},
])
} catch (e) {
if (e instanceof ReferenceError) {
reject(e)
}
}
})
}
/**
* Returns domain of the analytics tracking cookies (set with setCookieDomain()).
*/
export function getCookieDomain(): Promise<string> {
return new Promise((resolve, reject) => {
try {
push([
function (this: Tracker) {
resolve(this.getCookieDomain())
},
])
} catch (e) {
if (e instanceof ReferenceError) {
reject(e)
}
}
})
}
/**
* Returns the analytics tracking cookies path
*/
export function getCookiePath(): Promise<string> {
return new Promise((resolve, reject) => {
try {
push([
function (this: Tracker) {
resolve(this.getCookiePath())
},
])
} catch (e) {
if (e instanceof ReferenceError) {
reject(e)
}
}
})
}
/**
* Returns expiration time of visitor cookies (in milliseconds)
*/
export function getConfigVisitorCookieTimeout(): Promise<number> {
return new Promise((resolve, reject) => {
try {
push([
function (this: Tracker) {
resolve(this.getConfigVisitorCookieTimeout())
},
])
} catch (e) {
if (e instanceof ReferenceError) {
reject(e)
}
}
})
}
/**
* Sets the expiration time of referral cookies
*/
export function setReferralCookieTimeout(seconds: number) {
push([COOKIE_TRACK_EVENT.SET_REFERRAL_COOKIE_TIMEOUT, seconds])
}
/**
* Returns expiration time of session cookies
*/
export function getSessionCookieTimeout(): Promise<number> {
return new Promise((resolve, reject) => {
try {
push([
function (this: Tracker) {
resolve(this.getSessionCookieTimeout())
},
])
} catch (e) {
if (e instanceof ReferenceError) {
reject(e)
}
}
})
}
/**
* Sets the prefix for analytics tracking cookies. Default is "_pk_".
*/
export function setCookieNamePrefix(prefix: string) {
push([COOKIE_TRACK_EVENT.SET_COOKIE_NAME_PREFIX, prefix])
}
/**
* Sets the domain for the analytics tracking cookies
*/
export function setCookieDomain(domain: string) {
push([COOKIE_TRACK_EVENT.SET_COOKIE_DOMAIN, domain])
}
/**
* Sets the analytics tracking cookies path
*/
export function setCookiePath(path: string) {
push([COOKIE_TRACK_EVENT.SET_COOKIE_PATH, path])
}
/**
* Toggles the secure cookie flag on all first party cookies (if you are using HTTPS)
*/
export function setSecureCookie(secure: boolean) {
push([COOKIE_TRACK_EVENT.SET_SECURE_COOKIE, secure])
}
/**
* Sets the expiration time of visitor cookies
*/
export function setVisitorCookieTimeout(seconds: number) {
push([COOKIE_TRACK_EVENT.SET_VISITOR_COOKIE_TIMEOUT, seconds])
}
/**
* Sets the expiration time of session cookies
*/
export function setSessionCookieTimeout(seconds: number) {
push([COOKIE_TRACK_EVENT.SET_SESSION_COOKIE_TIMEOUT, seconds])
}
/**
* Sets cookie containing {@link https://developers.piwik.pro/en/latest/glossary.html#term-analytics-id | analytics ID} in browser
*/
export function setVisitorIdCookie() {
push([COOKIE_TRACK_EVENT.SET_VISITOR_ID_COOKIE])
}