Search code examples
javascriptexceptiontrackingprivacydo-not-track

Tracking DNT Exception handling


Currently Im trying to solve a problem about tracking.

On our website, an user should be able to decide whether or not he gets tracked and also which kind of information is shared.

Also we have different third party tracking mechanism, which can be switched off aswell. (if the user wants to)

I resolved this problem with local storage + cookies. If the user decides to deactivate one of our trackings, the code which is responsible for that kind of tracking will not be transmitted via the requested file.

Also I do respect the settings from window.Navigator.doNotTrack, which will in case of beeing 1, resolve in a snackbar popping up and asking for permission.

All Information I found about this topic is here

What I want to know is how to ask for an exception and also how to see whether or not the website is accepted or not.

I tried what is described here but it does not seem to work.

Navigator.trackingExceptionExists // will be undefined

Solution

  • The DNT header field is a mechanism for expressing the user's tracking preference in an HTTP request. At most one DNT header field can be present in a valid request.

    DNT-field-name = "DNT"

    DNT-field-value = ("0" / "1") *DNT-extension

    • A user agent MUST NOT generate a DNT header field if the user's tracking preference is not enabled.
    • A user agent MUST generate a DNT header field with a field-value that begins with the numeric character "1" if the user's tracking preference is enabled, their preference is for DNT:1, and no exception has been granted for the request target.
    • A user agent MUST generate a DNT header field with a field-value that begins with the numeric character "0" if the user's tracking preference is enabled and their preference is for DNT:0, or if an exception has been granted for the request target.
    • A proxy MUST NOT generate a DNT header field unless it has been specifically installed or configured to do so by the user making the request and adheres to the above requirements as if it were a user agent.

    Here are some Methods to Request a Site-specific Exception:

    1. void storeSiteSpecificTrackingException (StoreSiteSpecificExceptionPropertyBag properties)

      This is called by a page to store a site-specific tracking exception. The storeSiteSpecificTrackingException method takes a dictionary argument of type StoreSiteSpecificExceptionPropertyBag that allows optional information to be provided.

    2. DOMString? domain

      This is a cookie-domain to which the exception applies.

    3. DOMString? siteName

      A user-readable string for the name of the top-level origin.

    4. DOMString? explanationString

      A short explanation of the request.

    5. DOMString? detailURI

      A location at which further information about this request can be found.

    6. DOMString? expires

      A date and time, encoded as described for the cookie expires attribute indicating the maximum lifetime of the remembered grant.

    7. long? maxAge

      A positive number of seconds indicating the maximum lifetime of the remembered grant.

    8. sequence arrayOfDomainStrings

      A JavaScript array of strings. If the request does not include the arrayOfDomainStrings, then this request is for a site-wide exception. Otherwise each string in arrayOfDomainStrings specifies a target. When called, storeSiteSpecificTrackingException MUST return immediately. If the list arrayOfDomainStrings is supplied, the user agent MAY choose to store a site-wide exception. If it does so it MUST indicate this in the return value.

    So, as an answer to your first question how to ask for an exception ?, I would suggest you to do this :

    Navigator.storeSiteSpecificTrackingException
    

    And if you would like to remove the permission grants, then do this:

    Navigator.removeSiteSpecificTrackingException
    

    There is no callback for removeSiteSpecificTrackingException. After the call has been made, it is assured that there are no site-specific or site-wide exceptions for the given top-level origin.

    And as an answer to your second question how to see whether or not the exception preferences on a website is accepted or not ?, confirmSiteSpecificTrackingException is a method used for the same:

    boolean confirmSiteSpecificTrackingException (ConfirmSiteSpecificExceptionPropertyBag properties)
    

    This method is called by a page to confirm a site-specific tracking exception.

    So all that you have to do is:

    Navigator.confirmSiteSpecificTrackingException