为A/B测试设置Cookie



我正在尝试设置一个A/B测试,其中cookie将出现在50%的用户面前。我可以选择将cookie JavaScript放置在名为"的字段中;Head Include";目标页面的HTML头部,但cookie没有出现。以下是我的脚本:

document.cookie = 'cname=true; Secure';

当我将脚本代码放在域站点级别的HTML头中时,cookie就起作用了。我已经为cookie设置了一个显示在特定路径上的设置。参见以下

document.cookie = 'cname=true; Secure; path=/test';

但我不想使用上面的脚本,因为我想将cookie应用于只显示50%目标用户的目标页面,而不应用于指定路径的网站,因为这样cookie将100%显示该页面,从而破坏a/B测试的目的。

我的问题是,cookie是否可以设置在特定页面的HTML头上,而不是域的HTML头?(请注意,出于隐私原因,代码中对名称进行了概括(

我仍然不确定我是否遇到了问题

您正在使用某种CMS,此CMS可以选择在特定页面(目标页面上的"Head include"(或所有页面(域站点级别上的"HTML Head"(上包含脚本。第一个不起作用,后者起作用,但您不希望在所有页面上执行cookie脚本。

如果看不到(不工作的(目标页面生成的HTML代码,我们就无法找出它不工作的原因。如果你可以发布一个链接到一个准备好的页面(一个包含脚本但没有创建cookie的页面(,这可能有助于解决问题。

在不知道实际问题的情况下,另一种选择是将脚本发送到所有页面,但将实际执行过滤到特定的文件路径或url参数。示例:

// let current = new URL(location.href); // use this to get the current url of the browser
let current = new URL('https://www.someurl.net/some_directory/some_file.ext?a=yes&b=12345'); // just an example url
if (current.searchParams.has('a') && current.searchParams.get('a') === 'yes') {
// put your cookie script here, it will be executed only if the url has an a parameter and if that parameter has the value 'yes'
}

查看MDN,了解URL类提供的属性概述

最新更新