Cookie 如何跨网站追踪你的行踪

在现代互联网环境中,Cookie 作为一种重要的 Web 技术,不仅为用户提供了便利的会话管理和个性化体验,同时也成为了数字营销和用户行为分析的核心技术。虽然大多数用户了解 Cookie 能够记住登录状态和购物车信息,但很少有人深入理解**第三方 Cookie(Third-party Cookie)**是如何成为广告商和数据公司跨网站追踪用户浏览行为的技术基础。

本文将深入解析第三方 Cookie 的工作机制,揭示其在跨站点用户追踪中的核心作用,并探讨其对用户隐私的影响。

第一方 Cookie(First-party Cookie)

第一方 Cookie 是由用户当前访问的网站域名直接设置的 Cookie。其主要特征包括:

  • 域名归属CookieDomain 属性与当前访问网站的域名相匹配
  • 访问权限:只能被设置它的域名读取和修改
  • 主要用途:会话管理、用户认证、购物车状态保存、个性化设置等

示例场景

1
2
3
# 用户访问 https://myshop.com 时
# 服务器响应头设置 Cookie
Set-Cookie: session_id=abc123; Domain=myshop.com; Path=/; HttpOnly; Secure

在这个例子中,myshop.com 设置的 session_id 只能被 myshop.com 域名下的页面访问,这就是典型的第一方 Cookie

第三方 Cookie(Third-party Cookie)

第三方 Cookie 是由与当前访问网站不同的域名设置的 Cookie。其技术特点包括:

  • 域名差异CookieDomain 属性与当前页面域名不同
  • 嵌入式触发:通常通过嵌入的第三方资源(广告、分析脚本、社交插件等)设置
  • 跨站点持久性:能够在不同网站间保持标识符的一致性

技术实现机制

1
2
3
4
5
6
7
8
9
10
11
12
13
# 用户在 myshop.com 页面上
# 页面包含来自 ad-company.com 的广告图片
<img src="https://ad-company.com/ad?size=banner" />

# 浏览器向 ad-company.com 发起请求
GET /ad?size=banner HTTP/1.1
Host: ad-company.com
Referer: https://myshop.com/product/shoes

# ad-company.com 服务器响应并设置第三方 Cookie
HTTP/1.1 200 OK
Set-Cookie: tracking_id=user_123abc; Domain=ad-company.com; Path=/; Expires=...
Content-Type: image/png

跨网站追踪的技术实现机制

1. 用户标识符的建立

第三方 Cookie 追踪的第一步是为用户建立一个唯一标识符(Unique Identifier)。这个过程通常发生在用户首次访问包含第三方资源的网站时:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 第三方追踪脚本的典型实现
(function() {
// 检查是否已存在追踪 Cookie
let trackingId = getCookie('tracking_id');

if (!trackingId) {
// 生成新的唯一标识符
trackingId = generateUniqueId(); // 例如:user_789xyz123
setCookie('tracking_id', trackingId, {
domain: 'ad-company.com',
expires: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1年
sameSite: 'None',
secure: true
});
}

// 发送追踪信息到服务器
sendTrackingData(trackingId, {
referrer: document.referrer,
url: window.location.href,
timestamp: Date.now()
});
})();

2. 跨站点信息收集

当用户访问不同网站时,第三方 Cookie 能够通过以下机制收集跨站点行为数据:

HTTP Referer 头信息传递

1
2
3
4
5
6
7
8
9
10
11
# 用户从 myshop.com 访问包含 ad-company.com 资源的页面
GET /track.gif?page=product HTTP/1.1
Host: ad-company.com
Referer: https://myshop.com/product/running-shoes
Cookie: tracking_id=user_123abc

# 随后用户访问 news-site.com
GET /track.gif?page=article HTTP/1.1
Host: ad-company.com
Referer: https://news-site.com/sports/marathon-training
Cookie: tracking_id=user_123abc

JavaScript 接口数据收集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// 第三方追踪脚本收集的详细信息
const trackingData = {
userId: 'user_123abc',
sessionId: generateSessionId(),
pageData: {
url: window.location.href,
title: document.title,
referrer: document.referrer,
timestamp: Date.now()
},
deviceInfo: {
userAgent: navigator.userAgent,
screenResolution: `${screen.width}x${screen.height}`,
language: navigator.language,
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone
},
behaviorData: {
scrollDepth: calculateScrollDepth(),
timeOnPage: calculateTimeOnPage(),
clickEvents: getClickEvents()
}
};

3. 用户画像构建

追踪公司通过收集的跨站点数据构建详细的用户画像(User Profile)

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
{
"userId": "user_123abc",
"profile": {
"demographics": {
"estimatedAge": "25-34",
"gender": "likely_female",
"location": "San Francisco, CA"
},
"interests": [
"running",
"fitness",
"technology",
"online_shopping"
],
"visitedSites": [
{
"domain": "myshop.com",
"category": "e-commerce",
"visits": 15,
"lastVisit": "2024-01-15T10:30:00Z"
},
{
"domain": "news-site.com",
"category": "news",
"visits": 8,
"lastVisit": "2024-01-14T16:45:00Z"
}
],
"purchaseIntent": {
"categories": ["athletic_wear", "running_shoes"],
"priceRange": "$50-$150",
"likelihood": 0.75
}
}
}

技术实现的关键环节

1. 同源策略的绕过

虽然浏览器的**同源策略(Same-Origin Policy)**限制了不同域名间的直接数据访问,但第三方 Cookie 通过以下方式合法绕过这些限制:

  • 资源嵌入:通过 <img><script><iframe> 等标签嵌入第三方资源
  • SameSite 属性:设置 SameSite=None 允许跨站点发送 Cookie
  • Secure 标志:配合 HTTPS 确保 Cookie 的安全传输

大型追踪网络通常采用 **Cookie 同步(Cookie Syncing)**技术来扩大追踪覆盖范围:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Cookie 同步的实现示例
function syncCookies() {
const partners = [
'https://partner1.com/sync',
'https://partner2.com/sync',
'https://partner3.com/sync'
];

const myTrackingId = getCookie('tracking_id');

partners.forEach(partnerUrl => {
const syncImg = new Image();
syncImg.src = `${partnerUrl}?id=${myTrackingId}&partner=ad-company`;
// 这使得合作伙伴能够将他们的 ID 与我们的 ID 关联
});
}

3. 指纹技术的补充

Cookie 被阻止或删除时,追踪公司会使用**设备指纹(Device Fingerprinting)**技术作为补充:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 设备指纹生成示例
function generateDeviceFingerprint() {
const fingerprint = {
canvas: getCanvasFingerprint(),
webgl: getWebGLFingerprint(),
audio: getAudioFingerprint(),
fonts: getInstalledFonts(),
plugins: getPluginList(),
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
language: navigator.languages.join(','),
platform: navigator.platform,
hardwareConcurrency: navigator.hardwareConcurrency
};

return btoa(JSON.stringify(fingerprint));
}