# SDK
Consent Management
You should only redirect the user to our solution after obtaining their consent.
# What do you need?
Prerequisites
To use the First-id SDK, you must first have completed the backend implementation : Backend Implementation mentioned above.
You need to install our SDK on your website to use First-id.
URL of our SDK (SDK_LOADER_URL) :
- Production environment : https://cdn.first-id.fr/sdk/loader/loader.min.js
- Pre-production environment : https://cdn.preprod.first-id.fr/sdk/loader/loader.min.js
Simply use the following code on your website to initialize the SDK :
<script src="SDK_LOADER_URL"></script>
<script>
// You can wait the end of the initialization to get the result
document.addEventListener('firstId:initialized', (event) => {
console.log('First-id initialized:', event.detail)
// You can retrieve the First-id, it could be null
console.log('First-id:', event.detail.firstId)
// You can retrieve the First-id when you want thanks to
try {
FIRSTID.getId() // You can access to the First-id SDK with the global variable "FIRSTID", this method return the First-id value
} catch (e) {
// Can return an error if no cookie has been set
if (e.code === 'FirstIdCookieNotFound') {
console.log('First-id cookie not found')
}
}
});
// You can specify a configuration on the window.firstId
window.firstId = window.firstId || {
debug: false, // If you want to see the logs of the SDK
cookieName: 'firstid', // The name of the cookie where you put the First-id value
// User hashed email value
emailCookieName: 'hashed_email', // The name of the cookie where we can find the hashed email value after the user login
emailHashed: null, // OR the hashed email value of the user if you have it before the SDK initialization
emailGetterCallback: null, // OR a callback to get the hashed email value of the user
refreshCookieName: 'firstid_refresh', // the name of the cookie that SDK create to tell you to make a redirect to our gate to resync the First-id value
};
// You can add callbacks to execute when the SDK is loaded
window.firstId.callbacks.push(() => {console.log('First-id SDK loaded. #1')});
// you can add a callback to get the First-id value
window.firstId.callbacks.push(() => {
console.log('First-id SDK loaded #2')
try {
FIRSTID.getId() // You can access to the First-id SDK with the global variable "FIRSTID", this method return the First-id value
} catch (e) {
// Can return an error if no cookie has been set
if (e.code === 'FirstIdCookieNotFound') {
console.log('FirstID cookie not found')
}
}
});
// You can set the email hashed value of the user when you get it during his navigation
FIRSTID.setEmailHashed('<EMAIL_HASHED_VALUE>');
</script>
# User Flow
# User Doesn't Have the First-id Cookie
This is the common case : a user arrives on your website for the first time, they do not yet have the First-id cookie. So, after giving their consent, you redirect them to the Gate.
For the following example, let's assume the consent cookie name is "my_consent_cookie" and its value is "yes" when consent is given.
When the user gives consent on the CMP, you can :
- Set
"my_consent_cookie"with the value"yes"client-side via a JavaScript trigger on the "accept" button of the CMP. - If your CMP sets an easily readable cookie when consent is given, read that cookie instead of creating one specifically for us. On the next page, your backend will redirect to our Gate because :
- The user has given consent.
- The user does not yet have the First-id cookie.
# User Already Has the First-id Cookie
The user is logged into your website : you have their hashed email address. You can send it to our Gate to link the First-id cookie with the user's hashed email address.
So, after the user logs in, they will be redirected back to your website.
Then, in JavaScript, you can call First-id to set the hashed email of the user in SHA256 :
// load our SDK
<script src="SDK_LOADER_URL"></script>
<script>
// init the First-id SDK
// ...
// set the hashed email value in SHA256 of the user
FIRSTID.setEmailHashed('<EMAIL_HASHED_VALUE>');
// ...
</script>
# User Has a First-id Cookie, but a Previous First-id Was Linked to Their Email
In some cases, the user already has a First-id cookie, but another First-id token has already been linked to their hashed email address on another website.
To improve First-id performance, we need to resynchronize the First-id cookie with the previous First-id linked to the user's email address.
To do this, after calling the First-id SDK to set the hashed email, the SDK will check if the hashed email of the current user is already known. If so, we will compare the current First-id with the previously stored First-id for this hashed email.
If they are not identical, the SDK will create a cookie with the name "firstid_refresh" (you can customize this cookie name when initializing the SDK) and the value "true".
Then, you will need to check if this "firstid_refresh" cookie exists, and if its value is "true". If so, you will need to redirect the user to the Gate to resynchronize the First-id cookie with the previously linked First-id for their email address.
Error during IPV6 API request
In order to retrieve the user's IPV6, the First-id SDK makes a request to a dedicated endpoint. This request may generate a (failed)net::ERR_NAME_NOT_RESOLVED error.
This is due to the fact that the computer's network layer can't handle the IPV6 endpoint and, unfortunately, we can't know this until we try to call the endpoint.
This error is therefore expected, as the endpoint https://api-v6.prod.first-id.fr/info is configured to handle IPV6 only.
# Events
To monitor the loading or updates of the SDK, several events have been implemented:
firstId:available: fired at the end of SDK execution only if a First-id is available.event.detail.firstIdis always present. This is the recommended event for most integrations.firstId:initialized: fired at every end of SDK execution, whether a First-id is available or not.event.detail.firstIdmay benull.firstId:new: fired on first attribution of a First-id or on a value change. Not called if the First-id is already present and its value is unchanged.
// Common use case: retrieve the First-id when available
document.addEventListener('firstId:available',
(event) => console.log('First-id available:', event.detail.firstId) // always non-null
);
// Advanced use case: be notified at every end of SDK execution
document.addEventListener('firstId:initialized',
(event) => console.log('SDK initialized, First-id:', event.detail.firstId) // may be null
);
// Specific use case: react only when a new First-id is assigned
document.addEventListener('firstId:new',
(event) => console.log('New First-id assigned:', event.detail.firstId) // non-null, but not called if First-id already existed
);