setStorageType

Functionality

The setStorageType function allows the publisher to set an alternative solution to storing consent in cookies. Consent cookies currently consume about 3-4kb and are sent automatically by the browser to every request to the publisher’s main and sub-domains. This might cause problems as some APIs limit the size of HTTP headers. Additionally, it slows down requests of the publisher because of the additional overhead, which could be potentially an issue on mobile devices.

The setStorageType function allows the publisher to use the browser’s local storage as opposed to cookies. This has the advantage that it will not be included in publisher requests anymore, but the disadvantage is that it will only work on a single domain, i.e. sub-domains will require their own consent.

Syntax

function adconsent(command, parameter, callback)

argument name

type

optional

value

description

argument name

type

optional

value

description

command

string

 

'setStorageType'

 

parameter

integer

 

0, 1

0 - Cookie, 1 - Local Storage

callback

function

✔️

function(result:boolean, success:boolean)

The callback function that is called with the function call result.

parameter

This is an integer that can be either 0 or 1. A 0 value sets the consent storage to be in cookies, and a 1 value sets the storage to be the browser’s local storage. Please note that if for some reason local storage is unavailable in the browser (e.g., privacy options enabled in the browser blocking local storage), the storage will default to cookie.

Description

When configuring the storage type to be Local Storage, AdConsent doesn’t write any cookies storing consent anymore, storing the consent data in Local Storage instead. The other way holds true also, if we had consent data being stored in the browser Local Storage and we change the storage type to Cookie, AdConsent won’t write consent into Local Storage anymore, storing it in cookies.

The default storage is “Cookie”, so if no function is called, this is where the consent will be stored.

If we set the storage to be Local Storage and for some reason, the browser Local Storage is not available, the storage will fall back to cookies again.

Local storage naming

The local storage uses the same naming as the cookies but adds the prefix _sn_.

This means that the consent storage naming will be:

GDPR (EU)

  • euconsent-v2_sn_euconsent-v2

  • snconsent_sn_snconsent

CCPA (US - California)

  • usprivacy → _sn_usprivacy

PIPEDA (Canada)

  • caconsent_sn_caconsent

  • snconsent_sn_snconsent

US National

  • usconsent_sn_usconsent

Storage migration

When changing the storage type, consent is automatically migrated. This ensures that consent is migrated to a new storage, but also that the old storage is cleaned up when switching to a new storage.

When reading the consent and it does not exist on the selected storage type, AdConsent tries to read it from the alternative storage type. If the alternative exists, the value is read and stored in the primary storage type, and then the old storage data is deleted.

Scope

The function must be called before AdConsent sends its ready signal. Calls after AdConsent is ready will be ignored.

Example

adconsent('setStorageType', 1);