问题Updating(升级)DTM pre定义数据元素时,匿名函数 [英] Issue Updating DTM Pre-defined Data Element when Anonymous FUnction

查看:341
本文介绍了问题Updating(升级)DTM pre定义数据元素时,匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个名为CS测试作为这么pre定义数据元素:

 收益率(函数(){
  VAR右值=默认的自定义脚本的一些数据层的价值;
  返回右值;
})();
 

欲与基于事件基于规则的新值(例如,更新值)更新CS测试数据元素的值。我不能得到这个工作?我从previous后明白,当你显式调用_satellite.getVar(样本),操作的顺序是相反:

  1. 查找指定目标的类型(如路径(JS VAR)类型的JS对象),并返回一个值
  2. 查找卫星Cookie,并返回一个值
  3. 返回默认值

所以,不知道如何解决这个问题,因为数据元素code总是被执行,并返回一个值?

解决方案
  

以上我的观点是,更新卫星cookie的作品,但只有等到我所说的最后_satellite.getVar(CS测试),因为它会覆盖的数据元素code的值卫星饼干和返回值的数据元素。

好了,所以,我要制定出一些不同的想法在这里,是因为你还没有明确关于你的数据元素的功能确实,你正在努力实现全面,等等。所以也许有些什么这都将明确DTM如何工作的,并帮助你得到你需要的人。

设置数据元素的Cookie直接唯一的作品,用于更新它的时候DTM内部引用它,例如: (如前所述),随后的页面加载。另外,我觉得(但没有测试),它将在在规则领域使用%data_element%语法工作。

不过,这里提到 相同的链接我张贴在OP注释的),当你明确地打电话到 _satellite.getVar()它迫使你的数据元素的规则进行评估。这意味着,code你在那里(匿名函数)将得到执行,并尽一切可能做。

和它的声音的喜欢,也许你是想DTM以某种方式执行,但选择性地忽略了一些。好吧,那是不可能的DTM做。如果你想这样的事情发生,那么你就必须重写你的匿名函数来适应做类似的东西。但我会回到这一个时刻,因为首先我要退后一步,问。

为什么你甚至可以使用 _satellite.getVar()写入cookie的反正后,对不对?直接写入Cookie的一点是,你的旁路的数据元素。更重要的是,您已经拥有的价值,因为你设置cookie与它的。

那么,也许你只是巩固了你的code为了举例,而随后的 .getVar()通话是在一个单独的codeBOX或否则外,您设置Cookie的范围。

如前所述,当您对一个显式调用 .getVar(),操作的返回值DTM的顺序为

  1. 评估数据元素。这意味着你的匿名函数的意志 执行。如果返回一个truthy值,这就是你得到的。如果它 不返回truthy值。

  2. 如果它是一个持久性数据元,DTM查找 _sdsat_ [数据 元素名称] 的cookie。如果它存在(并具有truthy值),这是 你得到什么。如果没有的话..

  3. 返回您指定为数据的默认值的值 要素配置(或者不确定的,如果你没有指定任何东西)

所以,再一次,我有点在黑暗中拍摄在这里,因为我不知道你的数据元素,并与它的最终目标的细节,但它听起来像你对我希望能够调用 .getVar(),但有操作的顺序是这样的DTM是怎么做的页面加载(1和2是相反的)。那么,DTM不具有一个内置的方式做到这一点。这将是很好如果没有Adobe添加选项中的数据元素的配置来指定操作顺序,因为显然它的行为有所不同,具体取决于数据元素的引用(不管是由DTM自己或你)。但他们没有在这个时候。

所以,如果这是你想要的东西,那么你需要调整你的匿名函数的行为更像是DTM的行为与持久数据层。基本上,增加code它看起来为右值的默认值的cookie,然后的回落到你现在有什么。这样,就可以让你做出显式调用 .getVar(),因为现在你的匿名函数,它最先被执行时,会首先查找cookie的值。

另一种选择,可以..假设所有这code(即后来的麻烦 .getVar()调用)是相同的规则中,但对于不管是什么原因是你设置cookie摆在首位(例如,在一个单独的codeBOX)的范围内,可以将该值设置为不同的,在即时创建的数据元素,然后引用而不是外。做这种方式,而不是仅仅读取cookie的好处是,它暴露了它的数据元素,这样你也可以使用%data_element%的语法(从地方你把它设置,前进),如果您需要。

更新:

好了,从后续的意见,我与DTM玩弄,它看起来像你可以做到以下几点,这可能是你所期待的。

页范围的持久化

因此​​,例如在predefined数据元素,你可以有这样的事情:

 返回功能(右值){
  _satellite.data.customVars._defaultRvalue =
    右值
    ||
    _satellite.data.customVars._defaultRvalue
    ||
    从一些数据层默认的自定义脚本值
  ;
  返回_satellite.data.customVars._defaultRvalue;
}
 

随后,得到电流值,你应该这样做:

  _satellite.getVar('data_element')();
 

这将返回当前值或默认[..]如果是没有的。

然后,如果你想设置一个新值,你可以这样做:

  _satellite.getVar('data_element)(新价值);
 

这将返回新价值,如果你进行后续调用(页范围):

  _satellite.getVar('data_element')();
 

这将继续返回新价值。

示例场景:

  _satellite.getVar('data_element')(); //初始页面加载,则返回默认的[..]
_satellite.getVar('data_element')('富'); //返回'富'
_satellite.getVar('data_element')(); //返回'富'
_satellite.getVar('data_element')('巴'); //返回'吧
_satellite.getVar('data_element')(); //返回'吧
 

注意:这是仅适用于网页的时间。如果你需要它一页一页地坚持,你将需要自行设定,并从一个cookie借鉴。不要使用 _sdsat_ [data_element] 饼干,因为函数本身存储在cookie中。

会话+持久性(从页面加载持续到页面加载)

因此​​,对于坚持它在整个会话,一页一页地,例如,你可以这样做:

 返回功能(右值){
  VAR右值=
    右值
    ||
    _satellite.readCookie('右值')
    ||
    从一些数据层默认的自定义脚本值
  ;
  _satellite.setCookie('右值',右值);
  返回_satellite.readCookie('右值');
}
 

示例场景:

  _satellite.getVar('data_element')(); //初始页面加载,则返回默认的[..]
_satellite.getVar('data_element')('富'); //还是同一个页面上,退货'富'
_satellite.getVar('data_element')(); //还是同一个页面上,退货'富'
//重新加载页面或进入新的页面:
_satellite.getVar('data_element')(); //新的一页:回报'富'
_satellite.getVar('data_element')('巴'); //返回'吧
//重新加载页面或进入新的页面:
_satellite.getVar('data_element')(); //新的一页:返回巴
 

注意:了解这样做的一个主要需要注意的是,因为你有效地使 .setVar 方法的包装的功能,你会不能引用它的表单字段与%data_element%语法规则。那么,你的可以的,但它不会给你想要的东西。在我的测试中,它返回函数本身的字符串化版本,但谁知道怎么回事,可能反应,这取决于浏览器/版本。只是不这样做。

If I have a pre-defined data element called "CS Test" as so:

return (function() { 
  var rvalue = "default custom script value from some data layer";
  return rvalue;
})();

I want to update the value of "CS Test" data element with a new value (e.g. "Updated Value") based on an Event Based rule. I can't get this to work? I understand from a previous post that when you explicitly call _satellite.getVar('Example'), the order of operations is instead:

  1. Look for the specified target for the type (e.g. path (js var) for type JS Object) and return that value
  2. Look for the satellite cookie and return that value
  3. Return the default value

So not sure how to get around this since the data element code is always being executed and returning a value?

解决方案

My point above is that updating the satellite cookie works but only until I call the last _satellite.getVar("CS Test") because it overwrites the satellite cookie with the value of the Data element code and returns the value of the Data Element.

Okay so, I'm going to lay out a couple of different thoughts here, because you haven't been clear on what your data element function really does, what you are trying to achieve overall, etc.. so maybe some of this will both clarify how DTM works, and help you get to where you need to be.

Setting the data element's cookie directly only "works" for updating it when DTM internally references it, e.g. (as mentioned) subsequent page loads. Also, I think (but have not tested) that it will work when using %data_element% syntax in the rule fields.

But, as mentioned here (same link I posted in OP comment), when you explicitly make a call to _satellite.getVar() it forces your data element rule to be evaluated. This means that the code you have in there (your anonymous function) will get executed, and do whatever it does.

And it sounds like maybe you are wanting DTM to somehow execute it, but selectively ignore some of it. Well, that's not possible for DTM to do. If you want something like that to happen, then you must rewrite your anonymous function to accommodate doing something like that. But I'll get back to this in a moment, because first I want to step back and ask..

Why are you even using _satellite.getVar() right after writing the cookie anyway? The point of writing directly to the cookie is that you bypass the data element. More importantly, you already have the value, because you just set the cookie with it.

So, maybe you just consolidated your code for the sake of example, and that subsequent .getVar() call is in a separate codebox or otherwise outside the scope of where you set the cookie.

As mentioned, when you make an explicit call to .getVar(), DTM's order of operations for returning a value is

  1. Evaluate the data element. This means your anonymous function will execute. If this returns a truthy value, that's what you get. If it does not return a truthy value..

  2. If it is a persistent data element, DTM looks for the _sdsat_[data element name] cookie. If it exists (and has a truthy value), that's what you get. If it does not, then..

  3. Return the value you specified as a default value in the data element config (or undefined if you didn't specify anything)

So again, I'm kinda shooting in the dark here, because I don't know the details of your data element and ultimate goal with it, but it sounds to me like you want to be able to call .getVar() but have the order of operations be like how DTM does it on page load (1 and 2 are reversed). Well, DTM doesn't have a built-in way to do this. It would be nice if Adobe added options in the data element config to specify order of operations, since clearly it behaves differently, depending on how the data element is referenced (be it by DTM itself or by you). But they don't have that at this time.

So, if that is what you want, then you need to Restructure your anonymous function to behave more like DTM behaves with persistent data layers. Basically, add code to it that looks for the cookie for the default value of rvalue, then fall back to what you have now. This should then allow you to make explicit calls to .getVar(), because now your anonymous function, which gets executed first, will first look for the cookie value.

Another option may be.. assuming that all this code (namely, the troublesome subsequent .getVar() call) is within the same rule, but for whatever reason is outside the scope of where you set the cookie in the first place (e.g., in a separate codebox), you can set the value to a different, on-the-fly created data element and then reference that instead. The benefit of doing it this way instead of just reading the cookie is that it exposes it to a data element so that you can also use %data_element% syntax (from the place where you set it, moving forward) if you need to.

Update:

Okay so from followup comments, I was playing around with DTM, and it looks like you can do the following, which may be what you are looking for.

Page scoped persistence

So for example in the predefined data element, you can have something like this:

return function(rvalue) { 
  _satellite.data.customVars._defaultRvalue = 
    rvalue
    ||
    _satellite.data.customVars._defaultRvalue
    ||
    "default custom script value from some data layer"
  ;
  return _satellite.data.customVars._defaultRvalue;
}

Then, to get the current value, you'd do this:

_satellite.getVar('data_element')();

That will return the current value or "default[..]" if there is none.

Then, if you want to set a new value, you can do this:

_satellite.getVar('data_element')('new value');

This will return "new value", and if you make subsequent calls (page scope):

_satellite.getVar('data_element')();

It will continue to return "new value".

Example Scenario:

_satellite.getVar('data_element')(); // initial page load, returns "default[..]"
_satellite.getVar('data_element')('foo'); // returns 'foo'
_satellite.getVar('data_element')(); // returns 'foo'
_satellite.getVar('data_element')('bar'); // returns 'bar'
_satellite.getVar('data_element')(); // returns 'bar'

Note: This is only works for the duration of the page. If you need it to persist from page to page, you will need to instead set and draw from a cookie. Do NOT use the _sdsat_[data_element] cookie, because the function itself is stored in the cookie.

Session+ Persistence (persisting from page load to page load)

So for persisting it across a session, from page to page, you can do this for example:

return function(rvalue) { 
  var rvalue=
    rvalue
    ||
    _satellite.readCookie('rvalue')
    ||  
    "default custom script value from some data layer"
  ;
  _satellite.setCookie('rvalue',rvalue);
  return _satellite.readCookie('rvalue');
}

Example Scenario:

_satellite.getVar('data_element')(); // initial page load, returns "default[..]"
_satellite.getVar('data_element')('foo'); // still on same page, returns 'foo'
_satellite.getVar('data_element')(); // still on same page, returns 'foo'
// reload page or go to new page:
_satellite.getVar('data_element')(); // new page: returns 'foo'
_satellite.getVar('data_element')('bar'); // returns 'bar'
// reload page or go to new page:
_satellite.getVar('data_element')(); // new page: returns 'bar'

Note: A major caveat about doing this is that because you are effectively making the .setVar method a wrapper for a function, you will not be able to reference it in form fields in a rule with %data_element% syntax. Well, you can, but it will not give you what you want. In my testing, it returns a stringified version of the function itself, but who knows how else it may react, depending on browser/version. Just don't do it.

这篇关于问题Updating(升级)DTM pre定义数据元素时,匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆