Setting the path on Google Cookie Choices

Setting the path on Google Cookie Choices

We recently integrated the Google Cookie Choices into one of our corporate websites. I followed the simple instructions and it appeared to work as expected.

However, after close inspection by our QA guy, he found that the cookie was being set on multiple paths. For example: http://www.scoot.co.uk/England/Surrey/Godalming/MJP-Deans-DHMZ190.html and http://www.scoot.co.uk/find/solicitors-in-godalming.




Upon inspecting the cookies stored on my PC, it was clear to see that the script had stored 2 separate cookies, one with the path of "/find" and one with the path of "/England/Surrey..."

This was not what we wanted. We wanted the cookie to be stored on the root "/" so that once the cookie policy had been accepted on one page, it would not appear on any other page.

The Fix

The fix was easy. In the file: cookiechoices.js there is this snippet of code:


function _saveUserPreference() {
    // Set the cookie expiry to one year after today.
    var expiryDate = new Date();
    expiryDate.setFullYear(expiryDate.getFullYear() + 1);
    document.cookie = cookieName + '=y; expires=' + expiryDate.toGMTString();
}
To make the code save the cookie for the root of the domain, simply make this change:

function _saveUserPreference() {
    // Set the cookie expiry to one year after today.
    var expiryDate = new Date();
    expiryDate.setFullYear(expiryDate.getFullYear() + 1);
    document.cookie = cookieName + '=y; path=/; expires=' + expiryDate.toGMTString();
}


Adding "path=/;" was all that was needed. An easy fix.

Comments

Popular posts from this blog

Error Using Python Requests Library - TypeError: 'dict' object is not callable

Generating Best Selling Items with Amazon Product Advertising API