-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
534 lines (481 loc) · 17.3 KB
/
index.html
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Dittofeed Library Test</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
background-color: #f8f9fa;
color: #333;
}
h1 {
color: #2563eb;
margin-bottom: 30px;
text-align: center;
}
h2 {
color: #1e40af;
margin-bottom: 20px;
border-bottom: 1px solid #e5e7eb;
padding-bottom: 10px;
}
.container {
background-color: white;
border-radius: 10px;
padding: 25px;
margin-bottom: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}
.btn-group {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 25px;
}
button {
padding: 12px 18px;
background-color: #3b82f6;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: 500;
transition: background-color 0.2s;
flex: 1;
min-width: 120px;
}
button:hover {
background-color: #2563eb;
}
.form-group {
margin-bottom: 18px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #4b5563;
}
input[type="text"], textarea {
width: 100%;
padding: 10px;
border: 1px solid #d1d5db;
border-radius: 6px;
font-family: inherit;
font-size: 16px;
}
input[type="checkbox"] {
margin-right: 8px;
}
.response {
margin-top: 25px;
padding: 15px;
background-color: #ecfdf5;
border-left: 4px solid #10b981;
border-radius: 4px;
display: none;
font-weight: 500;
}
.checkbox-container {
background-color: #eff6ff;
padding: 12px;
border-radius: 6px;
margin-bottom: 20px;
border: 1px solid #dbeafe;
}
.checkbox-label {
display: flex;
align-items: center;
font-weight: 500;
margin-bottom: 0;
}
.anonymous-id-container {
background-color: #fffbeb;
padding: 15px;
border-radius: 6px;
margin: 20px 0;
border: 1px solid #fef3c7;
}
.anonymous-id-label {
font-weight: 500;
color: #92400e;
margin-bottom: 5px;
display: block;
}
.anonymous-id {
font-family: monospace;
word-break: break-all;
background-color: #fef3c7;
padding: 10px;
border-radius: 4px;
font-size: 0.9em;
}
.info-text {
font-size: 0.9em;
color: #64748b;
margin-top: 5px;
}
.refresh-btn {
background-color: #f59e0b;
font-size: 0.8em;
padding: 5px 10px;
margin-top: 10px;
}
.refresh-btn:hover {
background-color: #d97706;
}
.error-banner {
background-color: #fee2e2;
color: #b91c1c;
padding: 15px;
border-radius: 6px;
margin-bottom: 20px;
border: 1px solid #fecaca;
display: none;
}
.setup-instructions {
background-color: #f3f4f6;
padding: 15px;
border-radius: 6px;
margin: 20px 0;
font-size: 0.9em;
}
code {
background-color: #e5e7eb;
padding: 2px 5px;
border-radius: 4px;
font-family: monospace;
}
</style>
</head>
<body>
<h1>Dittofeed SDK Tester</h1>
<div id="error-banner" class="error-banner">
<strong>API Key Not Found!</strong> Please add your Dittofeed Write Key to the <code>.env</code> file to use this tester.
<div class="setup-instructions">
<p><strong>Setup Instructions:</strong></p>
<ol>
<li>Copy <code>.env.example</code> to <code>.env</code></li>
<li>Replace <code>YOUR_DITTOFEED_WRITE_KEY_HERE</code> with your actual API key</li>
<li>Restart the development server</li>
</ol>
</div>
</div>
<div class="container">
<h2>SDK Testing Panel</h2>
<div class="checkbox-container">
<label class="checkbox-label">
<input type="checkbox" id="use-identified" checked>
Use identified tracking (userId: "user-sdk-web")
</label>
</div>
<div class="anonymous-id-container">
<label class="anonymous-id-label">Current Anonymous ID:</label>
<div class="anonymous-id" id="current-anonymous-id">Initializing...</div>
<p class="info-text">This ID is used for anonymous tracking when no user ID is provided.</p>
<button class="refresh-btn" id="refresh-anonymous-id">Refresh ID Display</button>
</div>
<div class="form-group">
<label for="event-name">Event Name (for track):</label>
<input type="text" id="event-name" placeholder="e.g., Button Clicked">
</div>
<div class="form-group">
<label for="page-name">Page Name (for page):</label>
<input type="text" id="page-name" placeholder="e.g., Home Page">
</div>
<div class="form-group">
<label for="screen-name">Screen Name (for screen):</label>
<input type="text" id="screen-name" placeholder="e.g., Profile Screen">
</div>
<div class="form-group">
<label for="subscription-group">Subscription Group (for subscribe/unsubscribe):</label>
<input type="text" id="subscription-group" placeholder="e.g., marketing-emails">
</div>
<div class="form-group">
<label for="properties">Properties (JSON):</label>
<textarea id="properties" rows="3" placeholder='{"key": "value"}'></textarea>
</div>
<div class="btn-group">
<button id="identify-btn">Identify</button>
<button id="track-btn">Track</button>
<button id="page-btn">Page</button>
<button id="screen-btn">Screen</button>
<button id="subscribe-btn">Subscribe</button>
<button id="unsubscribe-btn">Unsubscribe</button>
<button id="flush-btn">Flush</button>
<button id="reset-anonymous-btn">Reset Anonymous ID</button>
</div>
<div id="response" class="response"></div>
</div>
<script type="text/javascript">
// The following is the standard Dittofeed snippet from documentation,
var _df = _df || [];
(function () {
var methods = ["track", "identify", "page", "flush", "subscribe", "unsubscribe", "getAnonymousId", "resetAnonymousId"];
methods.forEach(function (method) {
_df[method] = function () {
_df.push([method].concat(Array.prototype.slice.call(arguments)));
};
});
const config = window.DITTOFEED_CONFIG || {};
var script = document.createElement("script");
script.type = "module";
script.async = true;
script.setAttribute("data-host", "http://localhost:3001");
script.setAttribute("data-write-key", "Basic PLACEHOLDER_WRITE_KEY");
script.id = "df-tracker";
script.src = "http://localhost:5173/src/snippetEntry.js";
document.head.appendChild(script);
})();
// Additional application-specific code starts here
// ---------------------------------------------------
var dfInitialized = false;
// Helper function to update anonymous ID display
function updateAnonymousIdDisplay() {
const anonymousIdEl = document.getElementById('current-anonymous-id');
if (dfInitialized && typeof _df.getAnonymousId === 'function') {
try {
console.log("Attempting to get anonymous ID...");
const anonymousId = _df.getAnonymousId();
console.log("Retrieved anonymous ID:", anonymousId);
anonymousIdEl.textContent = anonymousId || 'No ID available';
} catch (err) {
console.error("Error getting anonymous ID:", err);
anonymousIdEl.textContent = 'Error retrieving ID: ' + err.message;
}
} else {
console.log("SDK not ready for getAnonymousId:", {
initialized: dfInitialized,
hasMethod: typeof _df.getAnonymousId === 'function'
});
anonymousIdEl.textContent = 'SDK not fully initialized yet...';
}
}
// Initialization and error handling
(function() {
// Listen for initialization error
const dfScript = document.getElementById('df-tracker');
if (dfScript) {
dfScript.addEventListener('error', function() {
document.getElementById('error-banner').style.display = 'block';
});
}
// Check when the SDK is fully loaded
var checkInterval = setInterval(function() {
if (typeof _df.getAnonymousId === 'function' && typeof _df.resetAnonymousId === 'function') {
clearInterval(checkInterval);
dfInitialized = true;
console.log('Dittofeed SDK fully initialized');
updateAnonymousIdDisplay();
}
}, 500);
// After 5 seconds, if not initialized, show error
setTimeout(function() {
if (!dfInitialized) {
document.getElementById('error-banner').style.display = 'block';
}
}, 5000);
})();
// UI interaction and event handlers
document.addEventListener('DOMContentLoaded', function() {
// Helper function to show response
function showResponse(message) {
const responseEl = document.getElementById('response');
responseEl.textContent = message;
responseEl.style.display = 'block';
setTimeout(() => {
responseEl.style.display = 'none';
}, 3000);
}
// Helper function to parse properties
function getProperties() {
const propertiesText = document.getElementById('properties').value.trim();
if (!propertiesText) return {};
try {
return JSON.parse(propertiesText);
} catch (e) {
showResponse('Error parsing properties JSON');
return {};
}
}
// Set up refresh button for anonymous ID
document.getElementById('refresh-anonymous-id').addEventListener('click', function() {
updateAnonymousIdDisplay();
showResponse('Anonymous ID display refreshed');
});
// Identify Button
document.getElementById('identify-btn').addEventListener('click', function() {
const useIdentified = document.getElementById('use-identified').checked;
const properties = getProperties();
if (useIdentified) {
_df.identify({
userId: "user-sdk-web",
traits: properties
});
} else {
_df.identify({
traits: properties
});
}
showResponse('Identify called');
});
// Track Button
document.getElementById('track-btn').addEventListener('click', function() {
const useIdentified = document.getElementById('use-identified').checked;
const eventName = document.getElementById('event-name').value.trim() || 'Test Event';
const properties = getProperties();
if (useIdentified) {
_df.track({
userId: "user-sdk-web",
event: eventName,
properties: properties
});
} else {
_df.track({
event: eventName,
properties: properties
});
}
showResponse(`Track called: ${eventName}`);
});
// Page Button
document.getElementById('page-btn').addEventListener('click', function() {
const useIdentified = document.getElementById('use-identified').checked;
const pageName = document.getElementById('page-name').value.trim() || 'Test Page';
const properties = getProperties();
if (useIdentified) {
_df.page({
userId: "user-sdk-web",
name: pageName,
properties: properties
});
} else {
_df.page({
name: pageName,
properties: properties
});
}
showResponse(`Page called: ${pageName}`);
});
// Screen Button
document.getElementById('screen-btn').addEventListener('click', function() {
const useIdentified = document.getElementById('use-identified').checked;
const screenName = document.getElementById('screen-name').value.trim() || 'Test Screen';
const properties = getProperties();
if (useIdentified) {
_df.screen({
userId: "user-sdk-web",
name: screenName,
properties: properties
});
} else {
_df.screen({
name: screenName,
properties: properties
});
}
showResponse(`Screen called: ${screenName}`);
});
// Subscribe Button
document.getElementById('subscribe-btn').addEventListener('click', function() {
const useIdentified = document.getElementById('use-identified').checked;
const subscriptionGroup = document.getElementById('subscription-group').value.trim() || 'marketing-emails';
const properties = getProperties();
if (useIdentified) {
_df.subscribe({
userId: "user-sdk-web",
subscriptionGroupId: subscriptionGroup,
properties: properties
});
} else {
_df.subscribe({
subscriptionGroupId: subscriptionGroup,
properties: properties
});
}
showResponse(`Subscribed to group: ${subscriptionGroup}`);
});
// Unsubscribe Button
document.getElementById('unsubscribe-btn').addEventListener('click', function() {
const useIdentified = document.getElementById('use-identified').checked;
const subscriptionGroup = document.getElementById('subscription-group').value.trim() || 'marketing-emails';
const properties = getProperties();
if (useIdentified) {
_df.unsubscribe({
userId: "user-sdk-web",
subscriptionGroupId: subscriptionGroup,
properties: properties
});
} else {
_df.unsubscribe({
subscriptionGroupId: subscriptionGroup,
properties: properties
});
}
showResponse(`Unsubscribed from group: ${subscriptionGroup}`);
});
// Flush Button
document.getElementById('flush-btn').addEventListener('click', function() {
_df.flush().then(() => {
showResponse('Flush completed');
}).catch(err => {
showResponse(`Flush error: ${err.message}`);
});
});
// Reset Anonymous ID Button
document.getElementById('reset-anonymous-btn').addEventListener('click', function() {
// If SDK is not initialized, we need to check periodically
const maxRetries = 10;
let retries = 0;
function attemptReset() {
if (typeof _df.resetAnonymousId === 'function') {
try {
const newId = _df.resetAnonymousId();
showResponse(`Anonymous ID reset: ${newId}`);
dfInitialized = true;
updateAnonymousIdDisplay();
} catch (err) {
showResponse(`Error resetting anonymous ID: ${err.message}`);
}
} else {
retries++;
if (retries < maxRetries) {
showResponse(`SDK initializing, please wait... (attempt ${retries}/${maxRetries})`);
setTimeout(attemptReset, 500);
} else {
showResponse('SDK initialization timeout. Please reload the page.');
}
}
}
if (dfInitialized) {
try {
const newId = _df.resetAnonymousId();
showResponse(`Anonymous ID reset: ${newId}`);
updateAnonymousIdDisplay();
} catch (err) {
showResponse(`Error resetting anonymous ID: ${err.message}`);
}
} else {
showResponse('SDK initializing, please wait...');
setTimeout(attemptReset, 500);
}
});
// Try to update anonymous ID display on load
updateAnonymousIdDisplay();
// Set interval to check for SDK initialization and update the anonymous ID display
if (!dfInitialized) {
const idCheckInterval = setInterval(function() {
if (dfInitialized) {
updateAnonymousIdDisplay();
clearInterval(idCheckInterval);
}
}, 1000);
}
});
</script>
</body>
</html>