-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
281 lines (229 loc) · 8.57 KB
/
Copy pathscript.js
File metadata and controls
281 lines (229 loc) · 8.57 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
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
// ========================================
// INDIE LANDING PAGE - JavaScript
// Form handling, analytics, tracking
// ========================================
// Configuration
const CONFIG = {
formEndpoint: 'https://formspree.io/f/YOUR_FORM_ID', // TODO: Replace with actual Formspree ID
matomoUrl: 'https://analytics.example.com/', // TODO: Replace with Matomo instance
matomoSiteId: 1,
};
// ========================================
// Email Form Handling
// ========================================
function setupEmailForms() {
const forms = document.querySelectorAll('.email-form');
forms.forEach(form => {
form.addEventListener('submit', async (e) => {
e.preventDefault();
const emailInput = form.querySelector('.email-input');
const submitButton = form.querySelector('.cta-button');
const email = emailInput.value.trim();
if (!isValidEmail(email)) {
showMessage(form, 'Please enter a valid email address.', 'error');
return;
}
// Disable form while submitting
submitButton.disabled = true;
submitButton.textContent = 'Submitting...';
try {
await submitEmail(email, form.id);
// Track conversion
trackEvent('Email Signup', 'Submit', form.id);
// Success
showMessage(form, '✓ You\'re on the list! Check your email for confirmation.', 'success');
emailInput.value = '';
// Redirect to thank you page after 2 seconds
setTimeout(() => {
window.location.href = 'thank-you.html';
}, 2000);
} catch (error) {
console.error('Form submission error:', error);
showMessage(form, 'Something went wrong. Please try again.', 'error');
} finally {
submitButton.disabled = false;
submitButton.textContent = form.id === 'heroForm' ? 'INITIATE_ACCESS' : 'SECURE_ALLOCATION';
}
});
});
}
function isValidEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
async function submitEmail(email, formId) {
// TODO: Replace with actual form submission endpoint (Formspree, Mailchimp, custom API)
// For now, log to console (development)
console.log('Email submitted:', { email, formId, timestamp: new Date().toISOString() });
// Simulate API call
return new Promise((resolve) => {
setTimeout(() => {
// Store in localStorage as backup
const submissions = JSON.parse(localStorage.getItem('emailSubmissions') || '[]');
submissions.push({ email, formId, timestamp: new Date().toISOString() });
localStorage.setItem('emailSubmissions', JSON.stringify(submissions));
resolve({ success: true });
}, 500);
});
/* Uncomment when using Formspree:
const response = await fetch(CONFIG.formEndpoint, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
email,
formId,
timestamp: new Date().toISOString(),
source: window.location.href
})
});
if (!response.ok) {
throw new Error('Form submission failed');
}
return await response.json();
*/
}
function showMessage(form, message, type) {
// Remove existing message
const existingMsg = form.querySelector('.form-message');
if (existingMsg) {
existingMsg.remove();
}
// Create new message
const msgDiv = document.createElement('div');
msgDiv.className = `form-message ${type}`;
msgDiv.textContent = message;
msgDiv.style.cssText = `
margin-top: 1rem;
padding: 0.75rem 1rem;
border-radius: 6px;
font-size: 0.9rem;
text-align: center;
background: ${type === 'success' ? '#00CC66' : '#FF6B6B'};
color: white;
`;
form.appendChild(msgDiv);
// Auto-remove after 5 seconds (if not success)
if (type !== 'success') {
setTimeout(() => msgDiv.remove(), 5000);
}
}
// ========================================
// Analytics & Tracking
// ========================================
function setupAnalytics() {
// Matomo Analytics (privacy-friendly)
if (CONFIG.matomoUrl && CONFIG.matomoSiteId) {
var _paq = window._paq = window._paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function () {
var u = CONFIG.matomoUrl;
_paq.push(['setTrackerUrl', u + 'matomo.php']);
_paq.push(['setSiteId', CONFIG.matomoSiteId]);
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.async = true; g.src = u + 'matomo.js';
s.parentNode.insertBefore(g, s);
})();
}
// Track scroll depth
setupScrollTracking();
// Track CTA clicks
setupCTATracking();
// Track FAQ interactions
setupFAQTracking();
}
function trackEvent(category, action, name) {
console.log('Event tracked:', { category, action, name });
// Matomo
if (window._paq) {
window._paq.push(['trackEvent', category, action, name]);
}
}
function setupScrollTracking() {
const thresholds = [25, 50, 75, 100];
const tracked = new Set();
window.addEventListener('scroll', () => {
const scrollPercent = (window.scrollY / (document.documentElement.scrollHeight - window.innerHeight)) * 100;
thresholds.forEach(threshold => {
if (scrollPercent >= threshold && !tracked.has(threshold)) {
tracked.add(threshold);
trackEvent('Scroll Depth', `${threshold}%`, window.location.pathname);
}
});
});
}
function setupCTATracking() {
document.querySelectorAll('.cta-button').forEach(button => {
button.addEventListener('click', () => {
const formId = button.closest('form')?.id || 'unknown';
trackEvent('CTA Click', 'Button Click', formId);
});
});
}
function setupFAQTracking() {
document.querySelectorAll('.faq-item').forEach((item, index) => {
item.addEventListener('toggle', () => {
if (item.open) {
const question = item.querySelector('.faq-question').textContent;
trackEvent('FAQ', 'Question Opened', `Q${index + 1}: ${question.substring(0, 50)}`);
}
});
});
}
// ========================================
// UTM Parameter Tracking
// ========================================
function trackUTMParameters() {
const params = new URLSearchParams(window.location.search);
const utmParams = {};
['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'].forEach(param => {
if (params.has(param)) {
utmParams[param] = params.get(param);
}
});
if (Object.keys(utmParams).length > 0) {
console.log('UTM Parameters:', utmParams);
localStorage.setItem('utm_params', JSON.stringify(utmParams));
// Track campaign visit
trackEvent('Campaign', 'Visit', utmParams.utm_campaign || 'unknown');
}
}
// ========================================
// Page Load Performance
// ========================================
function trackPagePerformance() {
window.addEventListener('load', () => {
setTimeout(() => {
const perfData = performance.getEntriesByType('navigation')[0];
if (perfData) {
const loadTime = perfData.loadEventEnd - perfData.fetchStart;
console.log('Page load time:', Math.round(loadTime), 'ms');
// Track if load time is slow
if (loadTime > 3000) {
trackEvent('Performance', 'Slow Load', `${Math.round(loadTime)}ms`);
}
}
}, 0);
});
}
// ========================================
// Initialize Everything
// ========================================
document.addEventListener('DOMContentLoaded', () => {
setupEmailForms();
setupAnalytics();
trackUTMParameters();
trackPagePerformance();
console.log('🚀 INDIE PLATFORM: SYSTEM_ONLINE');
console.log('📧 STORAGE_MODE: LOCAL');
console.log('📊 TELEMETRY: ACTIVE');
});
// ========================================
// Export for testing (if needed)
// ========================================
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
isValidEmail,
trackEvent
};
}