Dev.to AI 🤖 Ai 👁 0 📖 2 min read

Logging AI Assistant Referrals in Nginx With a Few Lines of Config

Marketing teams increasingly want to know how many visits come from ChatGPT, Perplexity, Gemini and Copilot. Analytics tools can show this, but they miss visitors who block scripts and they often lump referrers together.

Marketing teams increasingly want to know how many visits come from ChatGPT, Perplexity, Gemini and Copilot. Analytics tools can show this, but they miss visitors who block scripts and they often lump referrers together. A small change to your Nginx config gives you a raw, reliable count you can query with standard command-line tools.

Step 1: Classify the referrer with a map

map $http_referer $ai_source {
    default                         "";
    ~*chatgpt\.com                  "chatgpt";
    ~*chat\.openai\.com             "chatgpt";
    ~*perplexity\.ai                "perplexity";
    ~*gemini\.google\.com           "gemini";
    ~*copilot\.microsoft\.com       "copilot";
    ~*claude\.ai                    "claude";
}

Place this in the http block. The map is evaluated lazily, so it adds almost no overhead.

Step 2: Write a dedicated log

log_format ai_ref '$time_iso8601 $ai_source "$request_uri" "$http_referer" $status';

map $ai_source $log_ai {
    ""      0;
    default 1;
}

server {
    # ...
    access_log /var/log/nginx/ai_referrals.log ai_ref if=$log_ai;
}

Only requests with a recognised AI referrer are written, so the file stays small.

Step 3: Query it

Visits per source this week:

awk '{print $2}' /var/log/nginx/ai_referrals.log | sort | uniq -c | sort -rn

Top landing pages from Perplexity:

grep ' perplexity ' /var/log/nginx/ai_referrals.log | awk '{print $3}' | sort | uniq -c | sort -rn | head

Things to know

  • Not every AI visit has a referrer. Some apps open links without sending one. Treat these numbers as a floor.
  • UTM parameters help. Some assistants append utm_source to links. Capture $arg_utm_source in the same log format for a second signal.
  • Rotate the log. Add it to your logrotate config like any other access log.
  • Privacy. This log contains no IP addresses or user agents by design. Keep it that way unless you have a reason and a policy.

Turning visits into leads

Visit counts are only half the story. The useful question is which of these visits became enquiries. Pass the detected source into a hidden form field or a first-party cookie, and store it with the lead in your CRM. Then you can report AI-sourced leads, not just AI-sourced traffic.

We describe that CRM side in more detail in attributing AI search leads with UTMs and a CRM, including naming conventions and how to handle assistants that do not send referrers.

Published by PandaWM, an overseas marketing team based in Hong Kong.

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.