1

The W3C TPE document defines the TK header as follows: "The Tk response header field is a means for indicating the tracking status that applied to the corresponding request. An origin server is REQUIRED to send a Tk header field if its site-wide tracking status value is ? (dynamic) or G (gateway), or when an interactive change is made to the tracking status and indicated by U (updated). https://www.w3.org/TR/tracking-dnt/#response-header-field.

My question is how to set custom DNT http-headers on Amazon Cloudfront. My answer will show this can be done with a Lambda@edge function.

rvaneijk
  • 663
  • 6
  • 20

1 Answers1

0

Amazon Cloudfront supports lambda functions.

  1. Sign in to the AWS Management Console and open the AWS Lambda console
  2. Create a new lambda function
  3. Choose 'Edge Nodge.js 4.3' for the language
  4. Choose the cloudfront-modify-response-header template.
  5. Specify your CloudFront distribution and event to apply the function to.

Example code for a Tk header field for a resource that claims not to be tracking:

'use strict';
exports.handler = (event, context, callback) => {

    const response = event.Records[0].cf.response;
    response.headers['Tk'] = 'N';

    callback(null, response);
};

Example code for a Tk header field for a resource that claims to be tracking with consent:

'use strict';
exports.handler = (event, context, callback) => {

    const response = event.Records[0].cf.response;
    response.headers['Tk'] = 'C';

    callback(null, response);
};

Example code for conditional Tracking Status Value (TSV):

'use strict';
exports.handler = (event, context, callback) => {

    const request = event.Records[0].cf.request;
    const response = event.Records[0].cf.response;
    if (request.headers['DNT'] = '0') {
      response.headers['Tk'] = 'T';
    } else {
      response.headers['Tk'] = 'N';
    }

    callback(null, response);
};

PS: the answer is loosely based on (1) HSTS on Amazon CloudFront from S3 origin, https://serverfault.com/questions/820939/hsts-on-amazon-cloudfront-from-s3-origin and (2) AWS Lambda@Edge (Preview) http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html. See also https://www.w3.org/TR/tracking-dnt/#tracking-status-value.

rvaneijk
  • 663
  • 6
  • 20