Skip to content

Commit c8367d1

Browse files
feedback
1 parent c93c09a commit c8367d1

File tree

1 file changed

+41
-7
lines changed
  • src/content/docs/cloudflare-challenges/troubleshooting

1 file changed

+41
-7
lines changed

src/content/docs/cloudflare-challenges/troubleshooting/index.mdx

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,47 @@ Challenges are not supported by Microsoft Internet Explorer. If you are currentl
2222

2323
### Referer header
2424

25-
Your visitor's HTTP request contains a referer header set to the search engine that they came from. When they encounter and solve a Challenge Page, the request with the referer is sent to the origin, and the response to the request is served to the user. The JavaScript on the response page may read the value of `document.referer`, but it will not be accurate.
26-
27-
For example, a visitor to your website who is coming from a given search engine is challenged by a WAF rule via an interstitial Challenge Page served by challenges.cloudflare.com. Once the visitor loads the website’s home page, the `document.referer` value is no longer the search engine but challenges.cloudflare.com.
28-
29-
This affects tools like Google Analytics, which reads the referer from JavaScript, since it replaces the previous site that visitors came from.
30-
31-
You can add tracking scripts, such as the Google Tag Manager Javascript, within an existing [custom Managed Challenge Error Page](/rules/custom-errors/) to capture the correct referer header on the initial request.
25+
Your visitor's HTTP request contains a referer header set to the website that they came from. When they encounter and solve a Challenge Page, the request with the referer is sent to the origin, and the response to the request is served to the user. The JavaScript on the response page may read the value of `document.referer`, but it will not be accurate.
26+
27+
For example, a visitor coming from a given website is challenged by a [WAF rule](/waf/custom-rules/) via an interstitial Challenge Page served by your domain. Once the visitor loads the website's home page, the `document.referer` value is your domain, not the origin website.
28+
29+
This affects tools like Google Analytics, which reads the referer from JavaScript, since it replaces the previous website that visitors came from.
30+
31+
You can add tracking scripts, such as the Google Tag Manager Javascript, within an existing [Challenge Page](/rules/custom-errors/) to capture the correct referer header on the initial request.
32+
33+
```js title="Example JavaScript"
34+
<script>
35+
(function () {
36+
const gaIds = {
37+
"<YOUR_DOMAIN>": "<GA_TRACKING_ID>",
38+
};
39+
40+
const gaId = gaIds[window.location.hostname];
41+
42+
if (gaId) {
43+
const src = "https://www.googletagmanager.com/gtag/js?id=";
44+
45+
const gaScript = document.createElement("script");
46+
gaScript.src = src.concat(gaId);
47+
document.body.appendChild(gaScript);
48+
49+
window.dataLayer = window.dataLayer || [];
50+
function gtag() {
51+
dataLayer.push(arguments);
52+
}
53+
gtag("js", new Date());
54+
gtag("config", gaId);
55+
} else {
56+
console.warn(
57+
"Google Analytics ID not found for host:",
58+
window.location.hostname,
59+
);
60+
}
61+
})();
62+
</script>
63+
</body>
64+
65+
```
3266

3367
### Cross-origin resource sharing (CORS) preflight requests
3468

0 commit comments

Comments
 (0)