Marketing Use Cases - Cookies in Advertising

This guide explores how cookies are used in marketing and advertising, including pixel syncing, tracking, and audience building.

Common Marketing Scenarios

Scenario 1: Pixel Syncing

What It Is: Synchronizing user identifiers across different systems using tracking pixels.

How It Works:

  1. User visits advertiser's website
  2. Page loads tracking pixel from ad network
  3. Pixel request includes user ID from advertiser's cookie
  4. Ad network receives ID and syncs with their own cookie
  5. Both systems can now identify the same user

Example:

<!-- On advertiser site -->
<img src="https://tracker.adnetwork.com/sync?advertiser_id=user123" />

<!-- Ad network receives request -->
<!-- Sets cookie: Set-Cookie: ad_id=user123; Domain=adnetwork.com -->

Testing in Playground:

Scenario 2: Conversion Tracking

What It Is: Tracking when users complete desired actions (purchases, sign-ups, etc.)

How It Works:

  1. User clicks ad and lands on site (tracking pixel fires)
  2. Ad network sets cookie with campaign ID
  3. User completes conversion
  4. Conversion pixel fires with same campaign ID
  5. Ad network attributes conversion to campaign

Cookie Flow:

Ad Click → Set Cookie: campaign=campaign123
Conversion → Send Cookie: campaign=campaign123 → Attribution

Testing in Playground:

Scenario 3: Retargeting

What It Is: Showing ads to users who previously visited your site.

How It Works:

  1. User visits advertiser site
  2. Tracking pixel sets cookie with user segment
  3. User visits other sites
  4. Ad network reads cookie and shows relevant ads
  5. User sees ads based on previous visit

Cookie Example:

Set-Cookie: segment=interested_buyer; Domain=adnetwork.com; SameSite=None; Secure; Max-Age=2592000

Testing: Use cross-domain pixel loading to simulate retargeting

Scenario 4: Audience Building

What It Is: Creating user segments based on behavior across multiple sites.

How It Works:

  1. Multiple sites use same ad network
  2. Ad network sets cookie on each site visit
  3. Cookie accumulates user behavior data
  4. Ad network builds audience profiles
  5. Profiles used for targeted advertising

Cookie Attributes:

Set-Cookie: audience_data={...}; Domain=adnetwork.com; SameSite=None; Secure; Max-Age=63072000

Pixel Tracking Implementation

Basic Pixel Tag

Simple Tracking:

<img src="https://tracker.com/pixel?event=page_view&page=home" />

With Cookie Sync:

<script>
  var userId = getCookie('user_id');
  var pixelUrl = 'https://tracker.com/pixel?user=' + userId + '&event=page_view';
</script>
<img src="<%= pixelUrl %>" />

Sequence Diagram - Basic Pixel Tracking:

sequenceDiagram participant User participant Advertiser as Advertiser Site
(example.com) participant Tracker as Tracker Site
(tracker.com) participant Browser participant CloudWatch as CloudWatch Logs User->>Advertiser: Visit example.com/page Advertiser->>Browser: HTML with pixel tag Browser->>Browser: Parse HTML, detect tag Browser->>Tracker: GET /pixel?event=page_view&id=user123
Cookie: (if any from tracker.com) Tracker->>Tracker: Log request details
(params, headers, IP) Tracker->>CloudWatch: Write log entry Tracker->>Browser: HTTP 200 OK
Content-Type: image/gif
1x1 transparent GIF Browser->>Browser: Render pixel (invisible)

Advanced Pixel (JavaScript Beacon)

More Reliable (handles failures):

function sendPixel(url) {
  var img = new Image();
  img.src = url;
  // Also fires even if image fails to load
}

With Error Handling:

function trackEvent(event, data) {
  var params = new URLSearchParams({
    event: event,
    ...data,
    cookie: document.cookie
  });
  var pixel = new Image();
  pixel.onerror = function() {
    // Fallback tracking
    navigator.sendBeacon('/tracking', params.toString());
  };
  pixel.src = 'https://tracker.com/pixel?' + params.toString();
}

Sequence Diagram - Cookie Sync via Pixel:

sequenceDiagram participant User participant Advertiser as Advertiser Site
(shop.example.com) participant Tracker as Tracker
(tracker.adnetwork.com) participant Browser participant CloudWatch as CloudWatch Logs User->>Advertiser: Visit shop.example.com Advertiser->>Browser: HTML page Browser->>Browser: Store advertiser cookies
(first-party) Note over Browser: JavaScript executes Browser->>Browser: Read cookie: user_id=abc123
(from shop.example.com) Browser->>Tracker: GET /pixel?adv_user=abc123&event=purchase
Cookie: (tracker cookies if SameSite allows) Tracker->>Tracker: Extract adv_user from query params Tracker->>Tracker: Match with own user database Tracker->>Browser: Set-Cookie: tracker_id=xyz789
Domain=tracker.adnetwork.com
SameSite=None
Secure Tracker->>CloudWatch: Log sync event
{adv_user: abc123, tracker_id: xyz789} Tracker->>Browser: HTTP 200 OK
1x1 GIF Note over Browser,Tracker: Cookie sync complete
Both systems can identify same user

Flow:

  1. Advertiser redirects to sync endpoint
  2. Sync endpoint reads advertiser cookie
  3. Sync endpoint sets ad network cookie
  4. User redirected back to advertiser

Example:

User → advertiser.com → tracker.com/sync?adv_id=123 → tracker.com sets cookie → back to advertiser.com

Sequence Diagram:

sequenceDiagram participant User participant Advertiser as Advertiser
(advertiser.com) participant Tracker as Tracker
(tracker.com) participant Browser User->>Advertiser: Visit advertiser.com/page Advertiser->>Browser: Set-Cookie: adv_id=user123
Domain=advertiser.com Browser->>Browser: Store advertiser cookie Advertiser->>Browser: Redirect: 302 Found
Location: tracker.com/sync?adv_id=user123 Browser->>Tracker: GET /sync?adv_id=user123
Cookie: (advertiser cookie if same-site) Tracker->>Tracker: Read adv_id from URL params Tracker->>Tracker: Match adv_id with own user DB Tracker->>Browser: Set-Cookie: tracker_id=xyz456
Domain=tracker.com
SameSite=None
Secure Tracker->>Browser: Redirect: 302 Found
Location: advertiser.com/continue Browser->>Advertiser: GET /continue
Cookie: adv_id=user123 Note over Browser,Advertiser: Cookie sync complete
Both systems have user identifiers

Flow:

  1. Advertiser page loads pixel
  2. Pixel request includes advertiser cookie value
  3. Ad network reads cookie from request
  4. Ad network sets their own cookie

Example:

<img src="https://tracker.com/sync?adv_user=abc123" />
<!-- Tracker reads adv_user, sets own cookie -->

Sequence Diagram:

sequenceDiagram participant User participant Advertiser as Advertiser
(advertiser.com) participant Tracker as Tracker
(tracker.com) participant Browser User->>Advertiser: Visit advertiser.com/page Advertiser->>Browser: HTML with pixel: tracker.com/pixel?adv_user=abc123 Browser->>Browser: Execute JavaScript
Read cookie: adv_user=abc123 Browser->>Tracker: GET /pixel?adv_user=abc123
Cookie: (tracker cookies if any) Tracker->>Tracker: Extract adv_user from query params Tracker->>Tracker: Match with user database Tracker->>Browser: Set-Cookie: tracker_id=xyz789
Domain=tracker.com
SameSite=None
Secure Tracker->>Browser: HTTP 200 OK
1x1 GIF Note over Browser,Tracker: Cookie sync complete
Future requests include both cookies

Testing Pixel Syncing in Playground

Test 1: Basic Pixel Request

Procedure:

  1. Visit https://site-a.cookie-playground.pun7o.click
  2. Set cookie: user_id=test123
  3. Load pixel: <img src="https://cookie-playground.pun7o.click/pixel?user=test123">
  4. Check CloudWatch logs
  5. Verify cookie value in logs

Test 2: Cross-Domain Pixel Sync

Procedure:

  1. Visit https://cookie-playground.pun7o.click
  2. Set cookie with user identifier
  3. Visit https://site-a.cookie-playground.pun7o.click
  4. Load pixel from parent domain
  5. Check if cookie is sent with pixel request
  6. Verify in CloudWatch logs

Test 3: Query Parameter Passing

Procedure:

  1. Load pixel with query parameters:
    <img src="https://cookie-playground.pun7o.click/pixel?event=purchase&value=99.99&user=user123" />
  2. Check CloudWatch logs for parameters
  3. Verify all parameters logged correctly

Privacy and Compliance

GDPR Considerations

Requirements:

Implementation:

CCPA Considerations

Requirements:

Implementation:

Browser Restrictions

Current State:

Alternatives:

Real-World Examples

Example 1: E-commerce Retargeting

Scenario: User browses products but doesn't purchase

Cookie Flow:

  1. User visits shop.example.com
  2. Sets cookie: viewed_products=item1,item2,item3
  3. User leaves without purchase
  4. Later visits news.example.com
  5. Ad network reads cookie
  6. Shows ads for viewed products

Example 2: Lead Generation

Scenario: User downloads whitepaper

Cookie Flow:

  1. User visits company.com
  2. Downloads whitepaper (form submission)
  3. Sets cookie: lead_type=whitepaper_download
  4. User visits partner sites
  5. Cookie indicates qualified lead
  6. Shows relevant B2B ads

Example 3: Attribution Tracking

Scenario: Track which ad led to conversion

Cookie Flow:

  1. User clicks ad: adnetwork.com/click?campaign=summer_sale
  2. Sets cookie: campaign=summer_sale; source=google
  3. User lands on retailer.com
  4. User makes purchase
  5. Conversion pixel fires with campaign cookie
  6. Attribution: summer_sale campaign → conversion

Testing Scenarios in Playground

Scenario A: Simple Page View Tracking

  1. Visit playground domain
  2. Set cookie: visitor_id=abc123
  3. Load pixel: https://cookie-playground.pun7o.click/pixel?event=page_view
  4. Check CloudWatch logs
  5. Verify visitor ID in logs

Scenario B: Cross-Domain Tracking

  1. Visit https://site-a.cookie-playground.pun7o.click
  2. Set cookie: segment=high_value
  3. Visit https://cookie-playground.pun7o.click
  4. Load pixel from subdomain A
  5. Verify cookie behavior across domains
  1. Visit domain A
  2. Set cookie: advertiser_id=user456
  3. Load sync pixel: https://domain-b/pixel?sync=1&id=user456
  4. Check if domain B receives and can use the ID
  5. Simulate cookie matching

Future of Marketing Cookies

Declining Third-Party Cookies

Why:

Impact:

Alternatives

  1. First-Party Data: Collect data directly on your site
  2. Server-Side Tracking: Track via server logs
  3. Privacy Sandbox: Browser APIs (Topics API, FLEDGE)
  4. Contextual Advertising: Target based on content, not user
  5. Federated Identity: Login-based (e.g., Google, Facebook)

Migration Strategies

Short Term:

Long Term:

Best Practices

  1. Transparency: Disclose cookie usage clearly
  2. Consent: Obtain proper user consent
  3. Minimization: Collect only needed data
  4. Security: Use Secure and HttpOnly flags
  5. Expiration: Set appropriate cookie lifetimes
  6. Testing: Test cookie behavior across scenarios
  7. Compliance: Follow privacy regulations

Next Steps