# Akamai

# Gate integration

We're going to use Akamai to :

  • Redirect directly to the First-id system. The redirection will be performed if :
    • the cookie indicating that the user has given consent is present
    • a cookie containing the First-id identifier is not already present
    • FACULTATIVE : You have the user's SHA256 hashed email in a cookie
  • Set the cookie with the First-id ID obtained during redirection.
  • Redirect the user to the original page, removing the query param firstId from the URL, while keeping the other initial query params, as well as the hash fragments (#) of the URL.

Terminology

In the following implementation example, we consider that :

  • The cookie containing the user's consent is called : YOUR_CONSENT_COOKIE.
  • The mere presence of this cookie indicates the user's acceptance of the CMP; we do not check its value.
  • The cookie containing the identifier generated by the First-id system is called : firstid.

Here's a sequence diagram assuming that the consent cookie is set on your domain :

On Akamai, go to your account on the property corresponding to your website.

Click on the version currently deployed in the Manage Versions and Activations panel.

Manage Versions and Activations

Click on the Edit a new version button.

Manage Versions and Activations

We'll have to deal with 2 cases :

  • The user has a YOUR_CONSENT_COOKIE cookie and no cookie containing the First-id identifier.
  • The user has just been redirected by the First-id system and we need to clean up the URL and redirect him to his original page.

To do this, we'll need to define 4 variables.

In the Property Variables block, click on the "Add Variable" button at the end of the line.

Property Variables - bouton add variable

These 4 variables are as follows:

  • REDIRECT_URI_URL_ENCODE : This will contain the redirection URI to which we want to redirect the end user at the end of the process, encoded in urlEncode. This is the current page at the start of the process.
  • FIRSTID : Contains the unique First-id identifier received following redirection from the First-id system.
  • REDIRECT_HOST_URL_ENCODE : Contains the host to which we wish to redirect the end user at the end of the process, encoded in urlEncode. This is the host of the current page at the start of the process.
  • REDIRECT_URI : Contains the decoded redirection URI.

Variable naming

These variable names are completely arbitrary, and you are free to choose others. Just keep the following steps consistent.

URL encoding

The redirectHost and redirectUri parameters must be encoded in urlEncode to be correctly interpreted.

In the Property Configuration Settings section, we'll need to create two rules.

Click on the Add Rule button.

Property Configuration Setting - bouton add rule

Choose Blank Rule Template and name the First-id - Get ID.

Property rule - blank template

Click on Insert Rule.

We have three criteria to define that must be met for this rule to be triggered:

  • The user has a YOUR_CONSENT_COOKIE cookie
  • AND the user has no firstid cookie
  • Caller's user-agent is not a Bot

Cookie First-id terminology

In the following, we'll refer to a "First-id" cookie to avoid the term "cookie containing the identifier generated by the First-id system". But this cookie is by no means a third-party cookie. It is deposited on your main domain as a Secure first party cookie (see Cookie deposit).

HTTPOnly

To retrieve the firstid cookie in client-side JavaScript, we do not recommend setting the httpOnly attribute.

Select Match All in the selector next to the Criteria title and click on the Add Match button to add the first criterion.

Criteria Match All - Button add match

Configure the criterion as follows :

Rule - check cookie consent exist

Click the Add Match button again to add the second criterion and configure it as follows :

Rule - check cookie firstid not exist

Click the Add Match button again to add the third criterion, and configure it as follows :

Rule - check user agent not a bot

This is the list of crawlers in the example. Each line corresponds to a filtering rule in Regex form, applied to the HTTP User-Agent header :

  • "bot"
  • "google"
  • "facebook"
  • "crawl"
  • "yahoo"
  • "bing"
  • "curl"
  • "parse.ly"
  • "amp-validator"
  • "amphtml-validator"
  • "validator.amp.dev"
  • "seoPrisma"
  • "iframely"
  • "SpeedCurve"
  • "Convert NL Grabber"
  • "NL-Factory Grabber"
  • "outbrain"
  • "Bot"
  • "Google"
  • "Slurp"
  • "ia_archiver"
  • "Screaming Frog SEO Spider"
  • "grapeshot"
  • "ias_crawler"
  • "proximic"
  • "Qwantify"
  • "Pinterest"
  • "360Spider"
  • "Flipboard"
  • "weborama-fetcher"
  • "upday"
  • "babbar.tech"
  • "TTD-Content"
  • "Chrome-Lighthouse"

Bot Crawler

To avoid any SEO problems, we advise you to apply this criterion to avoid sending 302s to bots crawling your site. The list of bots present is not exhaustive, and we advise you to update it regularly.

Let's move on to the Behaviors. We'll need to :

  • Assign the current URI to our PMUSER_REDIRECT_URI_URL_ENCODE variable, encoding it as urlEncode
  • Assign the current host to our PMUSER_REDIRECT_HOST_URL_ENCODE variable, encoding it in urlEncode.
  • Redirect to the First-id system

Click on the Add Behavior button.

Behavior - button add behavior

In the modal that opens, select Set Variable you can also use the search engine.

Add behavior modal - set variable

Click on the orange Insert Behavior button at the bottom right of the modal.

Configure the behavior as follows :

Behavior - set variable URI

Akamai Variables

Note the use of the expression builtin.AK_URL, in fact AK_URL is one of the many variables provided by Akamai. You can find the full list in the Akamai documentation (opens new window)

Add a new behavior of type Set Variable and configure it as follows :

Behavior - set variable Host

Now that we've finished with the variables, we can move on to the redirection to the First-id system. To do this, add a behavior of type Redirect Plus :

Behavior - configure redirect

Here are the return addresses :

  • Production :

    https://gate.first-id.fr?redirectHost={{user.PMUSER_REDIRECT_HOST_URL_ENCODE}}&redirectUri={{user.PMUSER_REDIRECT_URI_URL_ENCODE}}

  • Preproduction :

    https://gate.preprod.first-id.fr?redirectHost={{user.PMUSER_REDIRECT_HOST_URL_ENCODE}}&redirectUri={{user.PMUSER_REDIRECT_URI_URL_ENCODE}}

The configuration of this first rule is complete 🎉.

Now let's move on to configuring the second rule. Click on the save button before going on to configure the second rule.

Click on the Add Rule button in the left-hand panel.

Choose Blank Rule Template and name it First-id - set cookie and redirect.

Click on Insert Rule.

We have two criteria to define that must be met for this rule to be triggered :

  • The user has a YOUR_CONSENT_COOKIE cookie.
  • AND the request contains a query param firstId we're in the case of a redirection made by the First-id system, which has added this query param.

WARNING

Note that the query param is called firstId in camelCase.

Criteria configuration :

Criteria - rule set cookie and redirect

In terms of behaviors, we're going to have 4 of them :

  • Store in the PMUSER_FIRSTID variable the identifier generated by the First-id system, which is passed in the query param firstId of the current query.
  • Assign the redirection URI contained in query param redirectUri to our PMUSER_REDIRECT_URI variable, decoding it the value is encoded in urlEncode.
  • Set the firstid cookie with the value of the PMUSER_FIRSTID variable.
  • Redirect the user to the original page by deleting query params firstId and redirectUri.

Store First-id identifier in PMUSER_FIRSTID :

Behavior - set First-id variable from query parmam

Store redirect URI and decode :

Behavior - set redirectUri variable from query parmam and decode

Set cookie containing First-id identifier :

Behavior - set First-id cookie

RGPD

In the example, we give a lifetime of 180 days to the cookie containing the First-id identifier. This duration is completely arbitrary. Make sure the duration of the cookie is the same as the duration of the consent the user has given you.

Behavior - redirect to original url

Now all you have to do is save and deploy this new version to activate the First-id service on your website 🎉.

Create a new rule named First-id - Refresh ID and configure it as follows :

Prerequisites :

  • You must have a PMUSER_EMAIL_HASHED variable in your property. This variable will contain the user's hashed email from the cookie.

# Criteria

TIP

The name of the firstid_refresh cookie used in the example can be customized during SDK initialization.

The name of the YOUR_EMAIL_COOKIE cookie used in the example should be replaced by the name of the cookie containing the user's SHA256 hashed email address.

Critères - rafraîchir le cookie maître

# Behaviors

Extracting the variables we need :

Comportement - set cookie firstid_refresh to expire

Behavior - set cookie firstid_refresh to expire

The purpose of this behavior is to delete the cookie requesting refresh. To do this, it sets a firstid_refresh cookie with an expiry date in the past.

Behavior - set cookie firstid_refresh to expire

This behavior redirects the user to the gate with the following parameters :

Comportement - Rediriger vers la porte

Redirection URL :

  • Production :

    https://gate.first-id.fr?redirectHost={{user.PMUSER_REDIRECT_HOST_URL_ENCODE}}&redirectUri={{user.PMUSER_REDIRECT_URI_URL_ENCODE}}&email={{user.PMUSER_EMAIL_HASHED}}

  • Preproduction :

    https://gate.preprod.first-id.fr?redirectHost={{user.PMUSER_REDIRECT_HOST_URL_ENCODE}}&redirectUri={{user.PMUSER_REDIRECT_URI_URL_ENCODE}}&email={{user.PMUSER_EMAIL_HASHED}}

# Using Health Check

First-id provides you with a Health Check endpoint to verify that the service is online. You can use this Health Check to monitor the correct operation of the First-id service.

To make sure you only redirect your user to First-id if the service is online, you can use an EdgeWorker to perform a Health Check on the First-id service.

EdgeWorker

An EdgeWorker is a JavaScript script that runs on Akamai's content distribution network. It allows you to customize the content and behavior of your website according to rules you define. It uses serverless technology : you don't need to manage the underlying infrastructure, Akamai takes care of that for you.

To do this, you need to create an Edge Worker :

Edge Worker - Create

Then click on the "Create EdgeWorker ID " button.

Complete the EdgeWorker creation form :

Edge Worker - Create

WARNING

The EdgeWorker name is completely arbitrary, so you can choose any name you like. Information such as "Resource tier" may vary depending on your contract with Akamai.

The use of EdgeWorker may incur an additional cost. If in doubt, contact your Akamai account manager.

Click on the "Create EdgeWorker ID " button.

Your Edge Worker appears in the list :

Edge Worker - ListClick on its ID, here 83169, to access its configuration.

Click on the orange "Create version " button to create a new version of EdgeWorker.

TIP

You can store your EdgeWorker code in a git repository and deploy it via a CI/CD pipeline.

Here we're going to code the Health Check in the online editor. To do this, click on the orange "Open editor " button.

Here's the online editor : Edge Worker - Code editor

Open the main.js file and copy the following code :


import { httpRequest } from 'http-request';
import { logger } from 'log';

// Check first-id service availabilty if health-check down (status_code, body content or timeout)
export async function onClientRequest (request) {
  const url = "https://healthcheck-ak.prod.first-id.fr/check-v2";
  const options = {};
  options.method = "GET";
  options.timeout = 500;
  try {
    const response =  await httpRequest(url,options);
    const http_status = response.status;
    const response_json = await response.json();
    const status = response_json.status;
    logger.log("http_status: %s", http_status);
    logger.log("status: %s", status);
    if ( http_status == 200 && status == "OK" ){
      request.setVariable('PMUSER_FID_AVAILABLE','1');
      logger.log("ok: %s", status);
    }
    else {
      request.setVariable('PMUSER_FID_AVAILABLE','0');
      logger.log("ko: %s", status);      
    }
  } catch(error){
    logger.log('error: %s', error);
    request.setVariable('PMUSER_FID_AVAILABLE','0');
  }
}

As a reminder, the URLs for our Health Check services are as follows :

  • Production: https://healthcheck-ak.prod.first-id.fr/check-v2
  • Preproduction: https://healthcheck-ak.preprod.first-id.fr/check-v2

Recommended endpoint: /check-v2 – reads a status updated every minute; more resilient to network latency. The /check endpoint remains available for backward compatibility.

We have created specific URLs for Akamai (hence the -ak suffix in the URLs) because the Edge Worker can only make HTTP requests to URLs exposed by Akamai.

Log

We use the log module to log information from our Health Check. To access these logs, refer to the Akamai documentation (opens new window). Learn more about the log module in the Akamai documentation. (opens new window).

Now edit the bundle.json file to modify the EdgeWorker version and give it a description :

{
  "edgeworker-version": "1.0",
  "description": "Implement First-id Health Check".
}

Click on the orange "Create version " button (at the very bottom of the page) to save your code.

A pop-up window will appear, asking you to confirm the creation of the version. Click on the "Create version " button.

Click on the "Activate version " button to activate this version of EdgeWorker.

Edge Worker - Activate version

A pop-up window appears to select the version to deploy, and the network on which to deploy it. Click on the "Activate version " button to confirm. Here we're taking version 1.0, which we'll deploy on the staging network to facilitate testing.

The Edge Worker deployed in staging can only be used on your staged properties.

Edge Worker - Activate version pop-up

Once we've finished deploying the EdgeWorker, we'll update your property to use it.

Edge Worker - Activate staging finish

Go to your property and create a new version.

In the "Property Variables " section, add a new variable PMUSER_FID_AVAILABLE.

This variable will be updated by the Edge Worker. If the First-id service is available, the variable will be set to 1, otherwise it will be set to 0.

In the Property Configuration Settings section, we need to create a rule via the "+ Rule " button.

Choose Blank Rule Template and name it First-id - Health Check.

Position the rule (by dragging it in the left panel) before the First-id - Get ID rule.

Property rule - health check position menu

Add the same criteria as the First-id - Get ID rule :

In behaviors, add a behavior of type Edgeworker :

Behavior - EdgeWorker

Then configure it as follows :

Behavior - EdgeWorker configuration

Select the EdgeWorker you've just created from the Identifier drop-down list.

Modify the First-id - Get ID rule to redirect the user to First-id only if the service is online. To do this, add a condition in the "Criteria " section :

Rule - Get ID - Health Check

Click on the orange "Save " button at bottom right to save your property, then deploy this new version. (In the example we've deployed the EdgeWorker only on the staging network).

You've now set up a Health Check to verify that the First-id service is online before redirecting the user to our service. 🎉

Important note

Variable values in your property are "scoped" for each query. This means that if you update the value of a variable during a query (which you do with the Edge Worker), this value will not be available in subsequent queries. See issue on Github (opens new window)

So for each visitor who meets the conditions for triggering the First-id - Get ID rule, the Edge Worker will perform a Health Check to verify that the First-id service is online.

Preserve User Referrer

To preserve the referrer of my user, I must take several actions described here.

Excluding Redirections

Warning, you should not perform redirections for the following file types:

jpe?g|gif|png|json|js|woff2?|map|css|svg|ico|bmp|xml|ttf|txt

Please add a filter on these file types in your configuration.