-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtn-engage.js
More file actions
246 lines (230 loc) · 10.2 KB
/
Copy pathbtn-engage.js
File metadata and controls
246 lines (230 loc) · 10.2 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
// BTN Engagement Tracking v2 -- shared across all Bankruptcy Tools Network sites
// Fires GA4 custom events: scroll, time, clicks, FAQ, CWV, copy, content groups
(function() {
if (typeof gtag !== 'function') return;
var host = location.hostname;
var path = location.pathname;
// 0. Content group dimension -- tag pages by category for cross-site comparison
var group = 'general';
if (/\/(check|screener|calculator|tool)/.test(path)) group = 'tool';
else if (/\/judges?\//.test(path)) group = 'judge-page';
else if (/\/districts?\/|\/states?\//.test(path)) group = 'district-guide';
else if (/\/caselaw|\/opinions?|\/precedent/.test(path)) group = 'caselaw';
else if (/\/(faq|glossary|timeline)/.test(path)) group = 'reference';
else if (/\/methodology|\/research|\/reports?\//.test(path)) group = 'research';
else if (/\/support|\/donate|ko-fi/.test(path)) group = 'support';
else if (/explainer|guide|how-to|what-is|chapter-\d/.test(path)) group = 'explainer';
else if (path === '/' || path === '/index.html') group = 'homepage';
gtag('set', { content_group: group, site_domain: host });
// 1. Scroll depth milestones
var scrollFired = {};
function getScrollPct() {
var h = document.documentElement.scrollHeight - window.innerHeight;
return h > 0 ? Math.round((window.scrollY / h) * 100) : 100;
}
window.addEventListener('scroll', function() {
var pct = getScrollPct();
[25, 50, 75, 100].forEach(function(m) {
if (pct >= m && !scrollFired[m]) {
scrollFired[m] = true;
gtag('event', 'scroll_depth', {
percent: m,
page_path: path,
site: host
});
}
});
}, {passive: true});
// 2. Time on page milestones (proves reading, kills bounce)
var timeFired = {};
var startTime = Date.now();
function checkTime() {
var elapsed = Math.floor((Date.now() - startTime) / 1000);
[15, 30, 60, 120].forEach(function(s) {
if (elapsed >= s && !timeFired[s]) {
timeFired[s] = true;
gtag('event', 'time_on_page', {
seconds: s,
page_path: path,
site: host
});
}
});
}
setInterval(checkTime, 5000);
// 3. Link clicks -- internal nav vs cross-network vs external
document.addEventListener('click', function(e) {
var a = e.target.closest('a[href]');
if (!a) return;
var href = a.href;
try {
var url = new URL(href, location.origin);
} catch (_) { return; }
// Screener conversion -- track clicks to 1328f.com/org as high-value conversions
if (/1328f\.(com|org)/i.test(href)) {
gtag('event', 'screener_click', {
event_category: 'conversion',
event_label: href,
source_page: path,
source_domain: host
});
}
if (url.hostname === host) {
// Internal nav within same site
gtag('event', 'internal_click', {
from_page: path,
to_page: url.pathname,
site: host
});
} else if (url.hostname.match(/1099cdebt\.com|109g\.org|1328f\.(com|org)|341meeting\.(com|org)|523a\.org|524injunction\.com|727a8\.(com|org)|automaticstay\.org|averagecreditcarddebt\.org|bankaccountlevy\.com|bankruptcycost\.(com|org)|bankruptcydenied\.com|bankruptcydischarge\.org|bankruptcydismissed\.com|bankruptcyexemptions\.org|bankruptcyexemptionsbystate\.com|bankruptcyfreshstart\.org|bankruptcyhardship\.org|bankruptcylien\.org|bankruptcymalpractice\.org|bankruptcymeanstest\.org|bankruptcymill\.(com|org)|bankruptcystudentloans\.org|bankruptcytaxes\.org|bankruptcytrustee\.org|behindonrent\.com|brunnertest\.org|buyacarafterbankruptcy\.com|buyahouseafterbankruptcy\.com|canifileagain\.org|canifilebankruptcytwice\.com|cantpaymybills\.com|ceaseanddesistdebt\.com|chapter13plan\.org|chapter7vs13\.org|codebtorstay\.org|cosignerrights\.org|creditcarddebtbankruptcy\.org|creditcardforgiveness\.org|creditcounselingbankruptcy\.org|creditscorebankruptcy\.com|debtbuyerrights\.org|debtcollectionlawsuit\.org|debtconsolidationvsbankruptcy\.com|debtrelieforbankruptcy\.com|debtsettlementvsbankruptcy\.com|debtstatuteoflimitations\.com|debttoincomeratio\.org|debtvalidationletter\.org|defaultjudgment\.org|deficiencybalance\.com|disabilityanddebt\.org|dischargebar\.org|dischargeinjunction\.(com|org)|dismissalrate\.org|dismissedbankruptcy\.org|divorceanddebt\.org|eidlbankruptcy\.com|eidlloanforgiveness\.com|filebankruptcyagain\.com|filebankruptcywithoutlawyer\.com|garnishedwages\.org|howmuchdoesbankruptcycost\.com|howtofilebankruptcy\.org|howtostopdebtcollectors\.org|judgmentproof\.org|kansascitybankruptcymill\.com|keepmycarinbankruptcy\.com|keepmyhouseinbankruptcy\.com|lienstripping\.org|meanstest\.org|medicalbillbankruptcy\.com|medicaldebtbankruptcy\.com|medicaldebtrights\.com|nondischargeable\.org|openbankruptcyproject\.(com|org)|paydayloandebt\.org|prosebankruptcy\.org|prosedebtors\.org|prosequestion\.com|reaffirmationagreement\.org|rebuildcreditafterbankruptcy\.com|relieffromstay\.org|righttocure\.org|saborrowerbankruptcy\.com|sbaborrowerbankruptcy\.com|sbaborrowersbankruptcy\.com|section1191\.org|section1192\.org|section1328\.org|section329\.org|serialfiler\.org|stopgarnishment\.org|studentloandefault\.org|studentloanforgive\.org|suedbycreditcardcompany\.com|suedfordebt\.org|vacatejudgment\.org|voluntarypetition\.org|whathappensinbankruptcy\.com|whatischapter7\.(com|org)|whatnottobeforebankruptcy\.com|zombiedebt\.org/)) {
// Cross-network link
gtag('event', 'network_click', {
from_site: host,
from_page: path,
to_site: url.hostname,
to_page: url.pathname
});
} else {
// External outbound
gtag('event', 'outbound_click', {
from_page: path,
site: host,
to_url: href.substring(0, 200)
});
}
});
// 4. FAQ accordion expand (details/summary elements)
document.querySelectorAll('details').forEach(function(d) {
d.addEventListener('toggle', function() {
if (d.open) {
var q = (d.querySelector('summary') || {}).textContent || '';
gtag('event', 'faq_expand', {
question: q.substring(0, 100),
page_path: path,
site: host
});
}
});
});
// 5. Page engagement signal at 10s (GA4 counts as "engaged session")
setTimeout(function() {
gtag('event', 'page_engaged', {
page_path: path,
site: host
});
}, 10000);
// 6. Copy-to-clipboard tracking -- signals high-intent users (attorneys, filers)
document.addEventListener('copy', function() {
var sel = (window.getSelection() || '').toString().trim();
if (sel.length > 5) {
gtag('event', 'content_copy', {
copied_length: sel.length,
copied_preview: sel.substring(0, 80),
page_path: path,
site: host
});
}
});
// 7. PDF / file download tracking
document.addEventListener('click', function(e) {
var a = e.target.closest('a[href]');
if (!a) return;
var href = a.href || '';
if (/\.(pdf|doc|docx|xls|xlsx|csv|zip)(\?|$)/i.test(href)) {
gtag('event', 'file_download', {
file_url: href.substring(0, 200),
file_name: href.split('/').pop().split('?')[0],
page_path: path,
site: host
});
}
});
// 8. Core Web Vitals -- LCP, CLS, INP (Google ranking factors)
// Uses PerformanceObserver API (no external library needed)
if (typeof PerformanceObserver !== 'undefined') {
// Largest Contentful Paint
try {
new PerformanceObserver(function(list) {
var entries = list.getEntries();
var last = entries[entries.length - 1];
if (last) {
gtag('event', 'web_vitals', {
metric: 'LCP',
value: Math.round(last.startTime),
page_path: path,
site: host,
rating: last.startTime < 2500 ? 'good' : last.startTime < 4000 ? 'needs-improvement' : 'poor'
});
}
}).observe({ type: 'largest-contentful-paint', buffered: true });
} catch (_) {}
// Cumulative Layout Shift
try {
var clsValue = 0;
new PerformanceObserver(function(list) {
for (var entry of list.getEntries()) {
if (!entry.hadRecentInput) clsValue += entry.value;
}
}).observe({ type: 'layout-shift', buffered: true });
// Report CLS on page hide
document.addEventListener('visibilitychange', function() {
if (document.visibilityState === 'hidden' && clsValue > 0) {
gtag('event', 'web_vitals', {
metric: 'CLS',
value: Math.round(clsValue * 1000),
page_path: path,
site: host,
rating: clsValue < 0.1 ? 'good' : clsValue < 0.25 ? 'needs-improvement' : 'poor'
});
}
});
} catch (_) {}
// Interaction to Next Paint (replaces FID)
try {
new PerformanceObserver(function(list) {
for (var entry of list.getEntries()) {
if (entry.interactionId) {
gtag('event', 'web_vitals', {
metric: 'INP',
value: Math.round(entry.duration),
page_path: path,
site: host,
rating: entry.duration < 200 ? 'good' : entry.duration < 500 ? 'needs-improvement' : 'poor'
});
}
}
}).observe({ type: 'event', buffered: true, durationThreshold: 40 });
} catch (_) {}
}
// 9. Conversion goals -- key actions that define success
// 3+ pages in session
var pageCount = parseInt(sessionStorage.getItem('btn_pages') || '0') + 1;
sessionStorage.setItem('btn_pages', pageCount);
if (pageCount === 3) {
gtag('event', 'conversion_multi_page', {
pages_viewed: pageCount,
site: host
});
}
// Ko-fi / support click
document.addEventListener('click', function(e) {
var a = e.target.closest('a[href]');
if (!a) return;
var href = a.href || '';
if (/ko-fi\.com|\/support|\/donate/i.test(href)) {
gtag('event', 'support_click', {
destination: href.substring(0, 100),
page_path: path,
site: host
});
}
// "File a complaint" or bar complaint links
if (/complaint|bar-complaint|ocdc|disciplinary/i.test(href) || /complaint/i.test(a.textContent)) {
gtag('event', 'complaint_link_click', {
destination: href.substring(0, 100),
page_path: path,
site: host
});
}
});
})();