使用 AppMeasurement 安装此插件
在实例化(使用 s_gi
)Analytics 跟踪对象后,将以下代码复制并粘贴到 AppMeasurement 文件中的任意位置。在您的实施中保留代码的注释和版本号可帮助 Adobe 对任何潜在问题进行疑难解答。
/******************************************* BEGIN CODE TO DEPLOY *******************************************/
/* Adobe Consulting Plugin: getValOnce v3.1 */
function getValOnce(vtc,cn,et,ep){var e=vtc,i=cn,t=et,n=ep; if(arguments&&"-v"===arguments[0])return{plugin:"getValOnce",version:"3.1"};var o=function(){if(void 0!==window.s_c_il){for(var e,i=0;i<window.s_c_il.length;i++)if((e=window.s_c_il[i])._c&&"s_c"===e._c)return e}}();if(void 0!==o&&(o.contextData.getValOnce="3.1"),window.cookieWrite=window.cookieWrite||function(e,i,t){if("string"==typeof e){var n=window.location.hostname,o=window.location.hostname.split(".").length-1;if(n&&!/^[0-9.]+$/.test(n)){o=2<o?o:2;var r=n.lastIndexOf(".");if(0<=r){for(;0<=r&&1<o;)r=n.lastIndexOf(".",r-1),o--;r=0<r?n.substring(r):n}}if(g=r,i=void 0!==i?""+i:"",t||""===i){if(""===i&&(t=-60),"number"==typeof t){var f=new Date;f.setTime(f.getTime()+6e4*t)}else f=t}return!!e&&(document.cookie=encodeURIComponent(e)+"="+encodeURIComponent(i)+"; path=/;"+(t?" expires="+f.toUTCString()+";":"")+(g?" domain="+g+";":""),"undefined"!=typeof cookieRead)&&cookieRead(e)===i}},window.cookieRead=window.cookieRead||function(e){if("string"!=typeof e)return"";e=encodeURIComponent(e);var i=" "+document.cookie,t=i.indexOf(" "+e+"="),n=0>t?t:i.indexOf(";",t);return(e=0>t?"":decodeURIComponent(i.substring(t+2+e.length,0>n?i.length:n)))?e:""},e){var i=i||"s_gvo",t=t||0,n="m"===n?6e4:864e5;if(e!==cookieRead(i)){var r=new Date;return r.setTime(r.getTime()+t*n),cookieWrite(i,e,0===t?0:r),e}}return""}
/******************************************** END CODE TO DEPLOY ********************************************/
使用此插件
getValOnce
函数使用以下参数:
vtc
(必需,字符串):要检查并确定之前是否已设置为相同值的变量cn
(可选,字符串):包含要检查的值的 Cookie 的名称。默认为"s_gvo"
et
(可选,整数):Cookie 的过期时间(以天或分钟为单位,具体取决于ep
参数)。默认值为0
,该值表示将在浏览器会话结束时过期ep
(可选,字符串):仅当还同时设置了et
参数时才会设置此参数。如果希望et
的过期时间以分钟而不是以天为单位,请将此参数设置为"m"
。默认值为"d"
,该值会以天为单位设置et
参数。
如果 vtc
参数与 Cookie 值相匹配,此函数将返回空字符串。如果 vtc
参数与 Cookie 值不匹配,此函数会将 vtc
参数作为字符串返回。
示例
// Prevent the same value from being passed in to the campaign variable more than once in a row for next 30 days
s.campaign = getValOnce(s.campaign,"s_campaign",30);
// Prevent the same value from being passed in to eVar2 more than once in a row for the browser session
s.eVar2 = getValOnce(s.eVar2,"s_ev2");
// Prevent the same value from being passed in to eVar8 more than once in a row for 10 minutes
s.eVar8 = getValOnce(s.eVar8,"s_ev8",10,"m");
版本历史记录
3.1(2022年9月22日)
- 修复了到期错误
3.0(2021 年 3 月 19 日)
- 以上下文数据形式添加了版本号。
2.01
- 修复了编写 Cookie 时的问题。
2.0
- 修正版本(重新编译,代码更小)。
1.1
- 添加了通过
t
参数选择有效期(分钟或天数)的选项。 - 更正了
k
变量的使用范围,以将该变量设为仅供此插件使用。此更改可防止页面上的其他代码可能造成的干扰。