Skip to content

Commit ac1ca11

Browse files
committed
chore: Choose osano variant based on country
1 parent 3b41092 commit ac1ca11

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,6 @@ yarn-error.log
8888
.devcontainer
8989

9090
src/images/infrastructure_screenshot_full_sonarqube-dashboard.webp
91+
92+
# Local Netlify folder
93+
.netlify

netlify.toml

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ package = "@netlify/plugin-gatsby"
44
[functions]
55
included_files = ["!.cache/data/datastore/data.mdb","!.cache/query-engine"]
66

7+
[[edge_functions]]
8+
path = "/*"
9+
function = "osano-country"
10+
711
[[headers]]
812
for = "/*"
913

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { HTMLRewriter } from 'https://ghuc.cc/worker-tools/html-rewriter/index.ts';
2+
3+
export default async (request, context) => {
4+
const response = await context.next();
5+
const hasGdpr = [
6+
'AT',
7+
'BE',
8+
'BG',
9+
'HR',
10+
'CY',
11+
'CZ',
12+
'DK',
13+
'EE',
14+
'FI',
15+
'FR',
16+
'DE',
17+
'GR',
18+
'HU',
19+
'IE',
20+
'IT',
21+
'LV',
22+
'LT',
23+
'LU',
24+
'MT',
25+
'NL',
26+
'PL',
27+
'PT',
28+
'RO',
29+
'SK',
30+
'SI',
31+
'ES',
32+
'SE',
33+
].includes(context.geo.country.code);
34+
35+
if (hasGdpr) {
36+
return (
37+
new HTMLRewriter()
38+
// .on('script', {
39+
// text(text) {
40+
// buffer += text.text;
41+
42+
// if (text.lastInTextNode) {
43+
// text.replace(buffer.replace(/variant=one/gi, 'variant=two'));
44+
// buffer = '';
45+
// } else {
46+
// text.remove();
47+
// }
48+
// },
49+
// })
50+
.on('script', {
51+
element(element) {
52+
const scriptSrc = element.getAttribute('src');
53+
if (
54+
typeof scriptSrc === 'string' &&
55+
scriptSrc.includes('cmp.osano.com/')
56+
) {
57+
element.setAttribute(
58+
'src',
59+
scriptSrc.replace(/variant=one/gi, 'variant=two')
60+
);
61+
}
62+
},
63+
})
64+
.transform(response)
65+
);
66+
}
67+
};

0 commit comments

Comments
 (0)