Skip to main content
  1. Blog Post/

#proTip - Proactively Cookies Auditing with Google Tag Manager and Google Analytics

2 min · 595 words
Table of Contents

Since GDPR was announced and more even with the rise of Safari's ITP and the other upcoming protection feature by the other major browsers, the concers about users privacy was grown which is really nice.

But despite browsers taking some actions to prevent the abuses, it's difficult to follow up the current cookies being used in our sites because we may end finding new ones that has been added by the developers, or some new pixel added by someone into our TMS.

For helping on this task, some weeks I launched Cookies.Report which is a online tool for checking the cookies generated by a site. It has some special features that will make the report more useful than just looking the cookies tab in our browser:

    Proactively Tracking Cookies

    This is going to be a easy code, we'll just to use the following snippet to pass a list of current cookies names into the user browsers into a variable and then we could pass it as a custom dimension in our pageview/event hits!.

    function(){
        document.cookie.split(';').map(function(c) {
            return c.split('=')[0].replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
        }).join("|");    
    }

    There's a point to have in mind, if it's the first time the user gets into the page it may happen that our pageview fires before our other javascripts are loaded and therefore we may be missing that data just on the landing pages.

    You could use a non-interactianal event that fires on DomReady of Window.load if this is a issue for you.

    Having the code needed to get the cookies list, we may think in some other more explicit workaround that may save hits. For example: We may generate a hash of the current cookies list, and just send an event each time that it changes, this way we'll only be sending the data once each time a new cookie is added.

    After we start receiving the data, we will be able to do/know a lot of interesting things.

      Hope someone find this useful in some way. Happy to listen to any comment you may have about this approach :)