-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
401 lines (355 loc) · 13.8 KB
/
main.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
$(document).ready(function () {
// Shared Vars
let paymentAvailable = false
let userData = JSON.parse(window.localStorage.getItem('userData'))
// Available in user page
// userData contains region in sent page
const userRegion = $("#userRegion").val()
// Mixpanel init
mixpanel.init("e8e1a30fe6d7dacfa1353b45d6093a00")
if (userData?.region === "US" || userRegion === "US") {
mixpanel.track_links(".rizz-button", "web_sent_tapped_new_app")
// Link visiter events
mixpanel.track_links(".download-link1", "web_tapped_get_your_own", {
text: $(".download-link1").text(),
ab_web_download_button_test_version: 3
})
mixpanel.track_links(".download-link2", "web_sent_tapped_get_your_own", {
text: $(".download-link2").text(),
ab_web_download_button_test_version: 3
})
// App user events
if (uid) {
mixpanel.track_links(".download-link1", "web_received_download", {
distinct_id: uid,
page: "user",
text: $(".download-link1").text(),
ab_web_download_button_test_version: 3
})
mixpanel.track_links(".download-link2", "web_received_download", {
distinct_id: uid,
page: "sent",
text: $(".download-link2").text(),
ab_web_download_button_test_version: 3
})
}
mixpanel.track_links(".another1", "web_sent_tapped_send_another_message")
}
$('.download-link').click(() => {
$.ajax({
url: '/api/pixel',
type: 'POST',
data: {
deviceId: window.localStorage.getItem('deviceId')
}
}).done(function (data) {
console.log('Pixel Response', data)
}).fail(function (err) {
console.log('Pixel Failed')
})
})
// Stripe init
const stripe = Stripe('find it yourself', { apiVersion: "2020-08-27" });
// const stripe = Stripe('find it yourself', { apiVersion: "2020-08-27" });
const paymentRequest = stripe.paymentRequest({
country: 'US',
currency: 'usd',
total: {
label: 'Boost Message',
amount: 99,
},
requestPayerName: true,
requestPayerEmail: true,
});
// Payment request
paymentRequest.canMakePayment().then(result => {
// mixpanel.track("payment_available_new", {
// paymentAvailable: result?.applePay ? 'applePay' : result?.googlePay ? 'googlePay' : result?.link ? 'link' : null
// })
if (result?.applePay) {
console.log('ApplePay Enabled')
paymentAvailable = 'applePay'
} else if (result?.googlePay) {
console.log('GooglePay Enabled')
paymentAvailable = 'googlePay'
} else if (result?.link) {
console.log('Link Enabled')
paymentAvailable = 'link'
} else {
console.log('no payment available')
}
})
// Listen for Payment
paymentRequest.on('paymentmethod', async (ev) => {
// Send request to /api/getPaymentIntent using jQuery ajax
const data = await $.ajax({
url: '/api/getPaymentIntent',
method: 'POST',
data: { questionId: userData.questionId }
});
// Confirm the PaymentIntent without handling potential next actions (yet).
const { paymentIntent, error: confirmError } = await stripe.confirmCardPayment(
data.clientSecret,
{ payment_method: ev.paymentMethod.id },
{ handleActions: false }
);
if (confirmError) {
if (userData?.region === "US") {
mixpanel.track("priority_inbox_payment_failed")
}
ev.complete('fail')
return alert("Your payment method failed. Try again or skip.")
}
if (paymentIntent.status === "requires_action") {
const { error } = await stripe.confirmCardPayment(data.clientSecret);
if (error) return alert("Your payment method failed. Try again or skip.")
}
ev.complete('success')
if (userData?.region === "US") {
mixpanel.track("priority_inbox_payment_succeeded")
}
$('.modal-container').addClass('off')
setTimeout(() => {
$('.modal-container').hide()
}, 300)
userData.isBoosted = true
window.localStorage.setItem('userData', JSON.stringify(userData))
isBoostedUI()
});
function isBoostedUI() {
$('.boost').addClass('button-translucent')
$('.boost').removeClass('button-white')
$('.boost').removeClass('pulse')
$('.boost').text(window.translations.boosted)
$('.boost').off("click")
}
// /p/sent logic
if (window.location.pathname.includes('p/sent')) {
// UI
$('.modal-container').hide()
$('.pfp').attr('src', userData?.ig_pfp_url)
if (userData?.isBoosted && false) isBoostedUI()
if (userData?.priorityInboxEnabled && userData?.paymentAvailable && false) {
$('.boost').show()
$('.boost').addClass('pulse')
// mixpanel.track("web_sent_seen_boosted_button")
} else {
$('.boost').hide()
$('.download-link').addClass('pulse')
}
// Handlers
$('.boost').click(() => {
if (userData?.region === "US") {
mixpanel.track("web_sent_tapped_boosted_button")
}
$('.modal-container').show()
$('.modal-container').removeClass('off')
})
$('.modal-bg, .priority-x').click(() => {
if (userData?.region === "US") {
mixpanel.track("web_sent_boosted_menu_tapped_hide")
}
// mixpanel.track("priority_inbox_skipped")
$('.modal-container').addClass('off')
setTimeout(() => {
$('.modal-container').hide()
}, 300)
})
$('.pay').click(async () => {
if (paymentAvailable) {
// let r = await $.ajax({
// url: '/api/stripe-checkout',
// type: 'POST'
// })
// window.location.href = r.url
if (userData?.region === "US") {
mixpanel.track("web_sent_boosted_menu_tapped_pay")
mixpanel.track("priority_inbox_payment_clicked")
}
paymentRequest.show()
} else {
if (userData?.region === "US") {
mixpanel.track("web_sent_boosted_menu_tapped_pay_failed")
}
alert('Please try again in a few seconds.')
}
})
} else {
// /username/game logic
window.localStorage.removeItem('userData')
}
// Asking question form
$('.form').submit(function (e) {
e.preventDefault();
if ($('#question').val().trim() === '') {
return alert('Please enter a question first!')
}
$('.submit').attr('disabled', true)
const userRegion = $("#userRegion").val()
if (userRegion === "US") {
mixpanel.track("web_tapped_send")
}
let referrer = document.referrer
if (navigator.userAgent.includes("Snapchat")) referrer = "https://snapchat.com"
let data = {
username: username,
question: $('#question').val(),
deviceId: $('.deviceId').val(),
gameSlug: gameSlug,
referrer: referrer
}
$.ajax({
url: '/api/submit',
type: 'POST',
data
}).done(function (data) {
console.log('Sent Question', data)
window.localStorage.setItem('userData', JSON.stringify({
questionId: data.questionId,
priorityInboxEnabled,
paymentAvailable,
ig_pfp_url,
ig_username,
region: data.userRegion,
}))
const userLanguage = $("meta[name='user:language']").attr("content")
let url = '/p/sent'
if (gameSlug) url += `/${gameSlug}`
if (userLanguage) url += `?lng=${userLanguage}`
if (uid) url += `&u=${uid}`
// Part of the AB test for reengagement messages
// To be deleted later
const downloadCopy = $(".download-link1").text()
if (userLanguage === "en" && userRegion === "US") {
url += `&ab_download_button=${encodeURIComponent(downloadCopy)}`
}
window.location.href = url
}).fail(function (err) {
console.log('submitted - failed')
console.log('Error submitting question', err);
alert('Internet error! Try again')
})
})
window.addEventListener("pageshow", function (event) {
var historyTraversal = event.persisted ||
(typeof window.performance != "undefined" &&
window.performance.navigation.type === 2);
if (historyTraversal) {
// Handle page restore.
$('.submit').attr('disabled', false)
$('textarea').val('')
$('.bottom-container').show()
$('.priority-modal').hide()
if (!(/android/i.test(userAgent))) {
$('.submit').hide()
}
}
});
const userAgent = navigator.userAgent || navigator.vendor || window.opera
const referrer = document.referrer
if (/android/i.test(userAgent)) {
// NGL Download Link
$('.download-link').attr('href', 'https://play.google.com/store/apps/details?id=com.nglreactnative')
// Rizz Download link
$('.rizz-button').attr('href', 'https://play.google.com/store/apps/details?id=com.rizz.android')
}
// If Snap preview, hide download button
// What makes something Snap preview?
// All must be true:
// referrer does not include Snapchat
// userAgent does not include Instagram
// userAgent contains iPhone
// userAgent does not contain Safari
// userAgent does not contain FBAN (Facebook)
// if (
// !referrer.toLowerCase().includes('snapchat') &&
// !userAgent.toLowerCase().includes('instagram') &&
// userAgent.toLowerCase().includes('iphone') &&
// !userAgent.toLowerCase().includes('safari') &&
// !userAgent.toLowerCase().includes('fban')
// ) {
// $('.bottom-container-user-wrapper').hide();
// }
$('textarea').focus(function () {
$('.bottom-container').hide()
})
$('textarea').blur(function () {
$('.bottom-container').show()
})
$('textarea').on('input', function (e) {
if (e.target.value == '' && !(/android/i.test(userAgent))) {
$('.submit').hide()
} else {
$('.submit').show()
}
});
if (!(/android/i.test(userAgent))) {
$('.submit').hide()
}
// DICE QUESTIONS
const APP_CDN_BASE_URL = "https://cdn.simplelocalize.io/57157aec81d54cb6b2a43f8b34a61d47/_production/";
const userLanguage = $("meta[name='user:language']").attr("content") || 'en';
let randomQuestions = []
$.get(APP_CDN_BASE_URL + userLanguage, function (data) {
const fakeQuestionKeys = Object.keys(data).filter(key => key.startsWith('FAKE_QUESTIONS.'))
randomQuestions = fakeQuestionKeys.map(key => data[key])
});
$('.dice-button').click(function (e) {
// Set textrea text to a random question
console.log(randomQuestions)
const randomQuestion = randomQuestions[Math.floor(Math.random() * randomQuestions.length)];
$('textarea').val(randomQuestion + ' ')
$('textarea').focus()
$('textarea')[0].selectionStart = randomQuestion.length + 1
$('textarea')[0].selectionEnd = randomQuestion.length + 1
$('.submit').show()
e.preventDefault()
})
if (!window.localStorage.getItem('deviceId')) {
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
window.localStorage.setItem('deviceId', uuidv4())
}
$('.deviceId').val(window.localStorage.getItem('deviceId'))
setInterval(() => {
let clickCount = parseInt($('.clickCount').text())
clickCount += Math.floor(Math.random() * 5) - 1
$('.clickCount').text(clickCount)
}, 800)
// Pageview
if (!window.location.pathname.includes('p/sent')) {
if (userRegion === "US") {
mixpanel.track("web_received_pageview", { "distinct_id": uid, referrer: window.document.referrer })
mixpanel.track("web_viewed_page", { ab_web_download_button: $(".download-link1").text(), ab_web_download_button_test_version: 2 })
}
$.ajax({
url: '/api/pageview',
type: 'POST',
data: {
uid: uid,
referrer: window.document.referrer,
game: gameId,
deviceId: window.localStorage.getItem('deviceId')
}
}).done(function (data) {
console.log(data)
})
$.ajax({
url: '/api/fingerprint/web',
type: 'POST',
data: {
deviceId: window.localStorage.getItem('deviceId')
}
}).done(function (data) {
console.log('Fingerprint Web', data)
})
}
// Show Rizz button if user agent contains "Snapchat"
if (navigator.userAgent.includes("Snapchat")) {
// Set .rizz-button to display: flex
$('.rizz-button').css('display', 'flex')
}
});