How to Count AI Referrals in Nginx and Apache Logs
Match the Referer host and utm_source=chatgpt.com in nginx or Apache logs to count AI assistant visits. Tested on a synthetic log, with output and limits.
- ai-referral-traffic
- server-logs
- nginx
- apache
- chatgpt
- utm-source
- referrer
To count AI referrals in server logs from nginx or Apache, take the Referer field of each successful page request, match its hostname against a list of assistant hostnames, and add requests whose URL carries utm_source=chatgpt.com. On a synthetic 418-line sample, a plain grep for chatgpt.com counted 65 lines, but only 52 were human ChatGPT page views.
As of 1 November 2026.
What do AI referrals look like in nginx server logs?
An AI referral is a normal request line whose Referer field names an assistant, whose URL carries a utm_source tag, or both. nginx's predefined combined format and Apache's combined format both log the referrer and user agent, so one set of commands reads either. Three lines from the synthetic sample used in this post (documentation IPs, user agent field left out):
203.0.113.88 - - [26/Oct/2026:00:11:32 +0000] "GET /blog/nginx-tuning?utm_source=chatgpt.com HTTP/1.1" 200 3906 "https://chatgpt.com/"
203.0.113.48 - - [26/Oct/2026:09:07:56 +0000] "GET /docs/api?utm_source=chatgpt.com HTTP/1.1" 200 2862 "-"
198.51.100.26 - - [26/Oct/2026:05:52:44 +0000] "GET /pricing/ HTTP/1.1" 200 27304 "https://perplexity.ai/"
Line 1 has a referrer and a tag. Line 2 has a tag and no referrer, the shape a link opened with noreferrer produces. Line 3 has a referrer and no tag. A filter that reads only one of the two signals loses lines 2 or 3.
The nginx documentation lists the predefined format as '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"', and says "If the format is not specified then the predefined “ combined ” format is used." (nginx docs, checked 1 November 2026). Apache documents "NCSA extended/combined log format" as "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" (Apache mod_log_config, checked 1 November 2026). If your log uses a custom format, the field positions in the commands below move.
So which hostnames go on the list?
Which hostnames should the filter match?
The filter should match the 22 hostnames in the AnswerTraffic referrer dataset, version 1.2.0 (last verified 30 October 2026), that are not marked ambiguous. The dataset has 24 rows. The two ambiguous rows, bing.com and google.com, also serve ordinary search traffic, so they cannot be told apart by hostname and are left out.
Do not hand-type the list. Build it from the open JSON file (ai-referrers.json, linked from the data page) so the filter follows the referrer list:
$ HOSTS=$(curl -s https://answertraffic.com/data/ai-referrers.json | jq -r '[.referrers[] | select(.ambiguous | not) | .hostname] | join(" ")')
$ echo "$HOSTS"
chatgpt.com chat.openai.com perplexity.ai www.perplexity.ai claude.ai gemini.google.com copilot.microsoft.com edgeservices.bing.com grok.x.com grok.com chat.deepseek.com www.meta.ai chat.mistral.ai you.com iask.ai copilot.com copilot.cloud.microsoft deepseek.com meta.ai phind.com duck.ai chat.qwen.ai
Not all 22 are equally solid. Counting only the non-ambiguous rows, 7 are backed by a third-party test and 15 are unverified (dataset evidence_type field). The tested seven are chatgpt.com, perplexity.ai, claude.ai, gemini.google.com, copilot.microsoft.com, www.meta.ai and chat.mistral.ai. The page for each hostname, such as the chatgpt.com entry, shows the evidence and its date.
Only two rows list a utm_source value: chatgpt.com (value chatgpt.com, third-party test) and copilot.com (value copilot.com, unverified). That is why the commands below treat utm_source=chatgpt.com as the only tag worth counting. Ahrefs reported in May 2025 that ChatGPT source links "add a URL parameter "?utm_source=chatgpt.com" to the URLs"; we have not read an OpenAI document that describes the tag. What does the filter find when you run it?
How many AI referrals do the commands count on a sample log?
On the synthetic sample, the filter counts 94 human page views from assistants: 77 with an AI referrer hostname and 17 with only the tag. The sample was generated by a script with a fixed seed, so every number here is reproducible. It is not traffic from any real site, and the shares in it say nothing about how real traffic splits.
Method box. Data: access-sample.log, 418 lines, nginx combined format, 26 to 28 October 2026 timestamps, synthetic. Counted: lines with method GET, status 200, a path that is not a static asset, and a user agent that is not an assistant fetcher or a crawler. Hostname compared exactly after stripping scheme and path, so chatgpt.com.example.net does not match. Tools: macOS awk (version 20200816) and BSD grep, then re-run on Linux with GNU Awk 5.3.2 (also in --posix mode), mawk and GNU grep 3.12, with identical output. Limit: the sample was built to contain the pitfalls, so it shows that the filter handles them, not how often they occur.
# ai-referrals.awk
BEGIN { n = split(hosts, h, " "); for (i = 1; i <= n; i++) ai[h[i]] = 1 }
{
split($2, q, " "); method = q[1]; path = q[2]
split($3, s, " "); status = s[1]
ref = $4; ua = $6
if (method == "GET" && status == 200) { } else next
if (path ~ /\.(css|js|png|jpg|svg|ico|woff2?)(\?|$)/) next
if (ua ~ /ChatGPT-User|Perplexity-User|Claude-User|[Bb]ot[\/;)]/) next
host = ref; sub(/^https?:\/\//, "", host); sub(/[\/?#:].*$/, "", host); host = tolower(host)
tagged = (path ~ /[?&]utm_source=chatgpt\.com([&#]|$)/)
if (host in ai) { kind = tagged ? "referrer+tag" : "referrer only"; cnt[host]++ }
else if (tagged) { kind = "tag only"; cnt["(no AI referrer, tagged)"]++ }
else next
by[kind]++; total++
}
With -F'"', awk splits each line at the double quotes, so $2 is the request, $3 holds status and bytes, $4 is the referrer and $6 the user agent. Run it with the $HOSTS list from the previous step (the full script also prints counts per day and per landing page):
$ awk -F'"' -v hosts="$HOSTS" -f ai-referrals.awk access-sample.log
35 chatgpt.com
17 (no AI referrer, tagged)
9 perplexity.ai
7 gemini.google.com
6 claude.ai
5 www.perplexity.ai
5 copilot.microsoft.com
3 chat.openai.com
2 www.meta.ai
2 chat.deepseek.com
1 grok.com
1 duck.ai
1 chat.mistral.ai
--- by signal
51 referrer only
26 referrer+tag
17 tag only
--- total 94
(Excerpt: the per-day and per-landing-page tables the script also prints are left out.)
The same three-way split of referrer, tag and both is the one why AI traffic shows as Direct describes for a published third-party sample. Here the tag-only row (17) is the part a referrer-only rule would have missed. An independent Python check returned the same 94, 77 and 17.
If you only need the ChatGPT tag, grep -c 'utm_source=chatgpt.com' access.log is enough, with one caveat covered next. Why did the plain grep overshoot by 13?
Why does a plain grep for chatgpt.com over-count?
A plain grep -c chatgpt.com over-counts because it matches the string anywhere in the line. On the sample it returned 65 lines against 52 human ChatGPT page views (35 with a referrer, 17 tag-only), a gap of 13 made of four separate mistakes:
Lines matched by grep chatgpt.com |
Count | Why it is not a human page view |
|---|---|---|
| Assistant fetcher requesting a tagged URL | 5 | User agent is ChatGPT-User, not a person |
| Redirect (status 301) with a ChatGPT referrer | 3 | The browser's follow-up request is counted too |
Referrer host chatgpt.com.example.net |
3 | Look-alike host, substring match only |
Referrer example.org/?ref=chatgpt.com |
2 | Hostname appears in a query string |
Each row was counted with its own command, and 5 + 3 + 3 + 2 = 13. Anchoring on the hostname, requiring status 200 and dropping fetchers is what closes the gap. The fetcher case matters most for the tag: 48 lines contain the tag, 43 once fetchers are removed. The sample contains tagged fetcher requests on purpose; we did not test whether real fetchers request tagged URLs.
Fetchers are the next question, because they are the largest non-human group in the sample.
How do you keep assistant fetchers out of the count?
Exclude by user agent. OpenAI documents ChatGPT-User: "When users ask ChatGPT or a CustomGPT a question, it may visit a web page with a ChatGPT-User agent." Perplexity documents Perplexity-User for the same purpose, and Anthropic documents Claude-User (all three checked 1 November 2026). Those requests show an assistant consulted a page, not that anyone clicked.
In the sample, the three fetcher agents account for 40, 12 and 5 lines, 57 in total, none carrying a referrer. The filter drops them with the user-agent pattern above. If you want the opposite view, fetches per URL, the log step in finding ChatGPT traffic in Direct counts ChatGPT-User requests; this post counts people.
The user agent is text the client sends about itself, and this post does not verify fetchers by IP. Treat the exclusion as a filter for your own reporting, not as bot detection. What remains uncounted?
What do server logs not show?
Server logs only show what the browser sent. When it sends no referrer and the link has no tag, the visit looks like any other direct visit. MDN documents the cause: the noreferrer link keyword "instructs the browser, when fetching the target resource, to omit the Referer header and otherwise leak no referrer information" (MDN, checked 1 November 2026).
Ahrefs reported in May 2025 that an in-content link in a paid ChatGPT account had a noreferrer attribute, while the same link type in a free account did not. The same test reported that the Perplexity desktop app "doesn’t seem to send referrers on anything" and that no referrer was passed from Copilot in Windows. These are one party's tests, not vendor statements. We have no tested source on in-app browsers, so we make no claim about them.
Report the result as a lower bound with its method. To see what one real click sends before you trust your rule, paste its URL and referrer into the referrer checker. To label the same visits inside GA4 instead, see the GTM custom dimension guide.
Common follow-up questions
Do the same commands work on an Apache access log?
Yes, if the log uses the combined format. Apache's documented combined format has the same field order as nginx's, with the referrer and user agent in the last two quoted fields. We ran the commands on an nginx-format sample, not on a real Apache log, so check one line of yours first.
How do I count rotated and compressed logs?
Decompress to standard output and pipe it in. We tested gzip -dc access-sample.log.1.gz | wc -l and it returned all 418 lines. The same pattern works with the awk: gzip -dc access.log.*.gz | awk -F'"' -v hosts="$HOSTS" -f ai-referrals.awk.
Should I count Perplexity or Claude by tag too?
Not from the dataset. Version 1.2.0 lists a tag value only for chatgpt.com (tested) and copilot.com (unverified), so for other assistants the referrer hostname is the signal. The ChatGPT tag explainer covers what the one tested tag means.
Sources
Nginx, ngx_http_log_module, https://nginx.org/en/docs/http/ngx_http_log_module.html, Last checked: 1 November 2026 Apache HTTP Server, mod_log_config, https://httpd.apache.org/docs/2.4/mod/mod_log_config.html, Last checked: 1 November 2026 MDN, rel="noreferrer", https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/noreferrer, Last checked: 1 November 2026 OpenAI, Overview of OpenAI crawlers, https://developers.openai.com/api/docs/bots, Last checked: 1 November 2026 Perplexity, Perplexity Crawlers, https://docs.perplexity.ai/guides/bots, Last checked: 1 November 2026 Anthropic, Does Anthropic crawl data from the web, https://support.claude.com/en/articles/8896518-does-anthropic-crawl-data-from-the-web-and-how-can-site-owners-block-the-crawler, Last checked: 1 November 2026 Ahrefs, Generative engines are breaking web analytics (May 2025 test), https://ahrefs.com/blog/generative-engines-are-breaking-web-analytics/, Last checked: 1 November 2026 AnswerTraffic, AI assistant referrers dataset v1.2.0, https://answertraffic.com/data/ai-referrers.json, Last checked: 1 November 2026 Own test: synthetic sample log and commands, 418 lines, run 1 November 2026 (own-data folder)
Checked 1 November 2026.