Certain types of content, such as API results or dynamically generated HTML, are nearly impossible for legacy CDNs to cache. Because Fastly acts as a proxy and has Instant Purging capabilities, we can cache assets that other CDNs can't. Still, there are some things, such as pages with tracking cookies, that are difficult to manage because they are inherently personal and user specific.
How Do Tracking Cookies Work?
When a user visits a site for the first time, they’ll be handed a cookie which uniquely identifies them:
Then, whenever that visitor returns to the site, this identifier can be read and aggregated to help measure behavior and patterns.
Using Tracking Cookies and Caching Content
Fastly uses a powerful scripting language called Varnish Configuration Language (VCL) that runs directly on our edge servers. VCL can handle very simple tasks, like adding and removing headers, as well as more sophisticated behavior such as programmable load balancing, url rewriting, and authentication.
Using VCL, customers can continue to use tracking cookies while still caching site content, improving site speed and reliability.
When the user makes a request to the site without a cookie Fastly triggers a request back to the site’s origin which will return a response with a cookie attached:
Fastly can then strip off the cookie, cache the content but then respond with the cookie reattached:
Then, whenever a particular user visits the site again, Fastly will strip off the cookie, send the user’s unique identifier back via our real-time logging system, and respond with the cached content and the reattached cookie.
The VCL needed to do this is below (note: you'll need to change 'mycookie' to be the name of your own cookie):
You would then need to create a new Logging object to send back req.http.Tmp-Set-Cookie, plus any other information you'd want.
Generating Tracking Cookies on the Edge
Taking things one step further, it's possible to avoid the initial call back to the origin by actually creating a cookie in VCL:
Either method allows Fastly customers to benefit from all the advantages of a CDN, including faster response times, spike protection and lower capital and operations expenditures, without losing the critical visitor behavior insights provided by tracking cookies.
If you'd like to activate custom VCL uploading for your account, just send an email to email@example.com.