# First-id retrieval on first page viewed

# Javascript redirection once CMP validated

Consent management

You should only redirect users to our solution after obtaining their consent.

Calling First-id as soon as the user confirms the CMP allows you to retrieve the identifier as quickly as possible, on the first page viewed.

Post CMP Redirection

Here's an example of code that will call the First-id service once the CMP has been validated :

  • JS Navigateur
<script>
    // Define a constant for the FirstID vendor ID (1178)
    const FIRSTID_VENDOR_ID = 1178;

    // This function listens for user consent via the TCF API
    function listenForTCFConsent(callback) {
        // Output to console that the system is now listening for TCF consent
        console.debug('Listening for TCF consent...');

        // Check if the TCF API (__tcfapi) is present on the window object
        if (!window.__tcfapi)
            return;  // Exit if TCF API is not found

        // Register an event listener to receive updates when consent is given through the TCF API
        window.__tcfapi('addEventListener', 2, (tcData, success) => {
            // Check if the API call was successful and GDPR applies
            if (success && tcData.gdprApplies) {
                // Check if the user has given consent for the specific vendor ID
                if (tcData.vendor.consents[FIRSTID_VENDOR_ID] && tcData.eventStatus === 'useractioncomplete') {
                    console.log('User consented to the flex feature.');
                    callback();  // Execute the callback function if consent is given
                } else {
                    console.log('User did not consent to the flex feature.');
                }
            } else {
                console.log('__tcfapi failed to load or is not applicable.');
            }
        });
    }

    // Call the listenForTCFConsent function with a callback function that reloads the page
    listenForTCFConsent(window.location.reload);
</script>

TIP

You can adapt this code to your own consent management logic (publisher purposes, custom vendor, ...).

Once you've redirected your user to our platform, he'll be redirected back to your infrastructure with the unique identifier First-id as a URL parameter, so you'll need to create the firstId cookie with the value of this parameter on the server side.

As explained in our documentation dedicated to Back implementation.