Developer forum

Forum » Development » Opt in for custom cookie categories

Opt in for custom cookie categories

Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

Hello, is there any way for me to request an opt in for a custom cookie category via the frontend?

I am using the three cookie opt in levels that you provide, 0 for rejecting all (both functional and tracking I suppose), 1 for accepting functional only and 2 for accepting both functional and tracking. But I want to categorize the tracking cookies further into analytical and marketing. Now I can create the categories but I cannot find any way for me to opt in on them from the frontend.

The solution I am on is using DW version 9.12.4 with Rapido in version 3.1.1 (and there is no way to upgrade this project to a new Rapido version)

Regards.

 


Replies

 
Nicolai Pedersen
Reply

Hi Unstein

Attached you can find the cookie dialog code coming from Swift and it itereates the custom categories that has been set up on a solution and makes it possible to make them part of the selection of cookies. You can also find the template on github:

https://github.com/dynamicweb/Swift/blob/main/Swift/Files/Templates/CookieWarning/Cookies.cshtml

You should be able to refactor that template to work with your Rapido solution. As you can see in the template, the optin is handled using fetch in js.

This is how it looks like:

And this is our setup of categories in Dynamicweb:

I also understand that you have an issue with the cookies set by the facet system in Rapido. A solution to that would be changing the JS to not use the cookies at all.

BR Nicolai

 
Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

So basically it is enough for me to have the ID of the inputs in a form as CookieCategory_@category and post to /Admin/Public/CookieOptInLevelConfig.aspx ?

 
Nicolai Pedersen
Reply

Yes, I belive that is correct.

 
Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

Is the following formData correct for me to set cookies in the category Analytical to the URL http://a4.local.dynamicweb.dk/Admin/Public/CookieOptInLevelConfig.aspx



The reason for me asking is that I get response.ok truthy but there is nowhere for me to confirm that this is actually working, I cannot see any OptInLevel cookie being set.

PS. To get the CookieCategory_<category_name> value to be included in the form data I had to change the name of the input to match the for value of the label. In the template you sent me the name of the input was just OptInCategory

Regards.

 
Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

Nicolai, any chance that you can help me with this? We have a customer that needs to have this working.

 

Regards.

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
Hi Unnsteinn,
The input name must be "OptInCategory". You also need to include a hidden input for "cmd" with value set to "SetCookieOptInLevel".
Here is a very basic example:
 
<form method="post" action="/Admin/Public/CookieOptInLevelConfig.aspx">
    <input type="hidden" name="cmd" value="SetCookieOptInLevel" />
    <input type="hidden" name="OptInLevel" value="1" />

    <p>Select cookie categories</p>
    @foreach (var category in GetLoop("Categories"))
    {
        var name = @category.GetString("Category");
        var selected = category.GetBoolean("Category.IsSelected");
        <p><label><input type="checkbox" name="OptInCategory" value="@name" checked="@selected" /> @name</label></p>
    }

    <input type="submit" />
</form>
However, you should use xhr for submitting the form instead.
You can also find sample templates in either /Files/Templates/CookieWarning or /Admin/Update/Files/Templates/CookieWarning
I have attached the templates here. Try the template named ModalDialogWarning_Categories.cshtml
 
Best regards,
Morten
 
Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

Thanks Morten for your reply, after applying your suggested changes I can see the Dynamicweb.CookieOptInLevel cookie being set with the value 1&Categories=Analytical,Marketing when I selected both of these categories in my frontend, However both of these categories do have cookies added to them in the cookie manager but they are never set in my browser



 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Do you have any code that actually sets the cookies "Marketing_FOO" and "Analytical_FOO"?
Adding cookies to a category does not mean that the cookies will be set in the browser. It just means that if the user has accepted those categories then we are allowed to set the cookies in those categories.

 
Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

Ok so by applying that logic, I could add the cookie _ga for google anayltics to the Analytics category as a custom cookie and then DW would not be able to set that cookie unless the user has opted in and approved the Analytics category right?

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

By default it will only prevent the cookie from being set by server side code that uses CookieManager.SetCookie(cookie).
If you have any client side cookies (e.g. google analytics) then you need to block the cookies in your template if the user has not accepted those cookies.

You can see how Swift handles this in the master template:

https://github.com/dynamicweb/Swift/blob/main/Swift/Files/Templates/Designs/Swift/Swift_Master.cshtml#L76
https://github.com/dynamicweb/Swift/blob/main/Swift/Files/Templates/Designs/Swift/Swift_Master.cshtml#L155

You should be able to adjust that to your needs.

 
Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

Ok, so to clarify, any cookies that are set by the client (like google analytics or facebook pixel) I will have to take care of myself? And If I would clear all my cookies and then render the page, the cookies would be set by google and facebook before the cookie modal being rendered, then I would accept or reject those cookies but it would be to late because they would already be set right ?

I am having a hard time understanding the purpose of the DW cookie categories at this point, at least in relation to analytical and marketing cookies.

Also the information for the google and facebook cookies are entered in the website settings inside DW, so I would have thought that DW templates would then have some logic to assist me in accepting / rejecting them (at least with the default tracking cookie category that DW provides), but that is not the case ?

Regards.
 

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Cookies which are set on the client side needs to be handled by client side code. Dynamicweb doesn't inject any code into your templates for handling this. You need to handle this yourself.
So you need to prevent the cookies from being set, if the user hasn't accepted the cookies yet. You need to do that in your templates (usually in the master template).
You can see an example of how this is handled for Google Tag Manager in Swift and then adapt it to your needs.
Different resources might require different solutions. You can use lazy evaluation of scripts, skip the resource completely, use a cookieless version of the resource or replace it with something else.

 
Nicolai Pedersen
Reply

Hi Unnstein

It is also what is possible and what is not. GA cookies for instance, is set by a script in the client that is hosted on another domain than your solution url. googletagmanager.com. This means that no code we can write - let it be server side or client side - can access or delete that information because that code will be on another host. So you HAVE to handle this like Morten describes.

Scripts like cookie information does exactly that as well. The cookie dialog javascripts prevents external scripts to even load by disabling the script tag - and only if you accept cookies, those scripts will load. The client side cookie javascript dialogs can on the other hand not block server side set cookies because they are sent before the client side javascript can even run. So if you use external cookie windows the problem just becomes opposite.

There is no super easy way to do this - except only load scripts with cookies if cookies has been accepted. And that logic has to be in the template.

There is an example of that in this file: https://github.com/dynamicweb/Swift/blob/main/Swift/Files/Templates/Designs/Swift/Components/Custom/CustomHeaderIncludeExample.cshtml

BR Nicolai

 
Unnsteinn Garðarsson
Unnsteinn Garðarsson
Reply

Ok so let´s say that I want to have these checks in my master template to only execute scripts if the user has given his consent. I can then use the GetCookieOptInCategories method from the CookieManager to see what categories have been accepted?

 

Regards. 

 
Nicolai Pedersen
Reply

Yes - if you use categories. Not all implementations do that - some just accept cookies or not (optinlevel=2)

So like this:;

  • optinlevel=0 (Do not accept cookies)
  • optinlevel=1 (do accept cookies in the categories X, Y, Z (this is where you can use GetCookieOptInCategories 
  • optinlevel=2 (accept all cookies)
 
Mike Nicolai Bjerregaard
Mike Nicolai Bjerregaard
Reply

Hi Nicolai,

Swift cookie manager.

It seems to be an issue with Dynamicweb.CookieOptInLevel, as if you chose to "accept selected".

it sets the Dynamicweb.CookieOptInLevel to:

Which should be the same result as "Accept all", where it sets Dynamicweb.CookieOptInLevel to: 2

Also, no matter if you select all or just some of the cookie categories, it sets to 0&Categories=on / on,on / on,on,on

Instead of 0, 1, 2 etc.

How would it be best to solve this propperly?

 

Best Regards

Mike

 

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Mike,

There seems to be some issues with the current template used in Swift.
The default template is /Files/Templates/CookieWarning/Cookies.cshtml

I have attached a modified version of that template which you can try instead.
Just rename the template file (remove .txt) and place it in the folder /Files/Templates/CookieWarning/
Then edit the website and select the new template in the "Cookies" dialog.

I'm not part of the Swift team, so use the attached template at your own risk :)
I have submitted a bug to the Swift team so that they can make the necessary changes to make it work as expected.

Best regards,
Morten

 
Mike Nicolai Bjerregaard
Mike Nicolai Bjerregaard
Reply

Hi Morten,

Thanks for your template version.

Seems to work alot better, i will test it out abit, thanks! :)

 

Best Regards

Mike

 

You must be logged in to post in the forum