-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmiddleware.html
More file actions
72 lines (67 loc) · 3.34 KB
/
Copy pathmiddleware.html
File metadata and controls
72 lines (67 loc) · 3.34 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>AJS Middleware – All-or-Nothing</title>
<!-- 1) Standard AJS shim -->
<script>
!function(){var i="analytics",analytics=window[i]=window[i]||[];
if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");
else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","screen","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware","register"];
analytics.factory=function(e){return function(){if(window[i].initialized)return window[i][e].apply(window[i],arguments);
var n=Array.prototype.slice.call(arguments);
if(["track","screen","alias","group","page","identify"].indexOf(e)>-1){
var c=document.querySelector("link[rel='canonical']");
n.push({__t:"bpc",c:c&&c.getAttribute("href")||void 0,p:location.pathname,u:location.href,s:location.search,t:document.title,r:document.referrer})
}
n.unshift(e);analytics.push(n);return analytics}};
for(var n=0;n<analytics.methods.length;n++){var key=analytics.methods[n];analytics[key]=analytics.factory(key)}
analytics.load=function(key,opts){var t=document.createElement("script");t.type="text/javascript";t.async=!0;
t.setAttribute("data-global-segment-analytics-key",i);
t.src="https://cdn.segment.com/analytics.js/v1/"+key+"/analytics.min.js";
var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(t,r);analytics._loadOptions=opts};
analytics.SNIPPET_VERSION="5.2.0";
analytics.page('Middleware Page');}}();
</script>
<!-- 2) Decide allow vs block ONCE (before any events) -->
<script>
(function decideAllOrNothing() {
const rawQs = window.location.search || "";
const ajsUid = new URL(window.location.href).searchParams.get('ajs_uid');
const hasPercentInRaw = /[?&]ajs_uid=[^&]*%/.test(rawQs);
const hasPercentInDecoded = !!(ajsUid && ajsUid.includes('%'));
// If either check hits, we BLOCK ALL events
window.__AJS_BLOCK_ALL__ = (hasPercentInRaw || hasPercentInDecoded);
console.log('[AJS] Block-all mode:', window.__AJS_BLOCK_ALL__, { ajsUid, rawQs });
})();
</script>
<!-- 3) Register middleware (applies to ALL payloads) -->
<script>
analytics.addSourceMiddleware(({ payload, next }) => {
if (window.__AJS_BLOCK_ALL__) {
console.warn('[AJS MW] Blocking ALL events due to invalid ajs_uid');
return; // drop page, track, identify, etc.
}
next(payload); // allow everything
});
</script>
<!-- 4) Load AJS, then send events inside ready() -->
<script>
analytics.load("G5BwWiwo7E5XCw85h43neqDn59j6KCTp");
analytics.debug(); // see logs in console
analytics.ready(function () {
console.log('[AJS] ready – sending events');
analytics.page('Middleware Page');
analytics.track('Test Event', { example: 'all-or-nothing' });
analytics.identify('user_123', { plan: 'pro' });
});
</script>
</head>
<body>
<h1>AJS Middleware – All-or-Nothing</h1>
<p>
Allow mode: open without <code>ajs_uid</code> or with a clean value.<br>
Block mode: open with <code>?ajs_uid=10114337733694088%0D%0A</code>.
</p>
</body>
</html>