How to set page level variables in adobe DTM? - javascript

I have used the adobe analytics for the tracking purpose in my website, but unfortunately I couldn't able to set the variables definition to that.
My code:
var z = new Object();
z.abc = true;
z.def.ghi = true
I am defining a variable like the above format, but I am unable to set it. Any help on this.?

There is a checkbox in script editor which gives you the option to run the code globally. That will do it.

You cannot set z.def.ghi unless you first define z.def as an object. For example:
var z = new Object();
z.abc = true;
z.def = {};
z.def.ghi = true

Related

Document variables not recognized by fields. Acrobat Pro

I hope this is the right place for this question. I have been trying to set up a simple document that calculates fields based on other fields with the same document. I am attempting to create a list of variables to make scripting faster. right now my document script looks like this
var lv = this.getField("Level").value;
var s = this.getField("StrB").value;
var d = this.getField("DexB").value;
var c = this.getField("ConB").value;
var int = this.getField("IntB").value;
var w = this.getField("WisB").value;
var ch = this.getField("ChaB").value;
var ss = this.getField("Str").value;
var ds = this.getField("Dex").value;
var cs = this.getField("Con").value;
var ints = this.getField("Int").value;
and so on.
This is an example of a script using these variables
{
event.value = strBonus + pb;
}
This returns nothing. Not even a warning. I double-checked my spelling and names. I am sure this is a formatting issue as I am a novice and don't fully understand the language. Any help would be greatly appreciated! Thank you for your time in reading this.
When you define and initialise a variable in a document level script, they get the field value at that time (and that's normally blank).
So, in order to simplify things, define the variables as Field Objects, such as in
var lv = this.getField("Level") ;
In the calculation, you will then retrieve the field value as, for example, in
var lv1 = lv.value * 5 ;
And that should do it.
About documentation: You will need the Acrobat JavaScript documentation, which is part of the Acrobat SDK documentation, downloadable from the developer section of the Adobe website.

GTM javascript does not fill variables

I am trying to use predefined variables for a simple GTM javascript macro, however, the variables do not load. Why is my code not loading the variables?
code:
function () {
var value = {{Click URL}};
var begin = value.indexOf(":")+1;
var end = value.substr(-1);
return value.slice(begin,end);
}
Just a shot in the dark, but the click variables are not enabled by default. Check if they are enabled in variables/built in variables:
You'd need to set the checkbox in front of Click URL.
In the end I solved it by leaving out the "end" variable (slice automaticlly uses the last character if you do not feed it the second argument). It looks like this now:
function () {
var value = {{Click URL}};
var begin = value.indexOf(":")+1;
return value.slice(begin);
}

javascript: save characters/digits without libraries

I use a lot of the following expressions in my code:
document.
.getElementsBy...
.querySelector...
I need to save characters without using any libraries. That can be done by
var d = document;
Then, instead of document. I can write d. now.
I am wondering if there is a simple way to do the same thing for methods
.getElementsBy... and .querySelector....
Since these have a variable term, I cannot put the entire thing into a
variable, like var q = .querySelector(".class"), because the .class
changes almost every time.
You can create functions to avoid adding properties to the document object as shortcut if you don't want to.
function gEBI(d,id)
{
return d.getElementById(id);
}
function qS(d,s)
{
return d.querySelector(s);
}
var d = document;
var ele1 = gEBI(d,"yourID");
var ele2 = qS(d,".class");
You can make your own shortcut functions-references manually.
document.gEBI = document.getElementById;
document.gEBI(id);
But it's not a good practice to make such shortcuts.

How can I get the content of a variable that I don't know its name

Although this question is about datatables, it's not specifically about it.
I have datatables init stored in variables. The variable name varies as I have several datatables on the page. I am trying to collect the content of those variable by assembling the variable's name and I don't know how i can later use the 'string' I've assembled as a variable
For example:
var var_1_id_0 = $('.item1').datatable();
var var_1_id_1 = $('.item2').datatable();
var var_1_id_2 = $('.item3').datatable();
// later in the code.
var varname = 'var_1_id'+'_0';
// varname now holds the string 'var_1_id_0' which is the first variable.
My question is how can I use varname's string 'var_1_id_0' as the variable 'var_1_id_0'?
I hope that make sense.
Thanks
It is not possible. The closer solution is this one:
var datatable={};//A new Object.Arrays doesn't work property for this.
datatable['var_1_id_0'] = $('.item1').datatable();
datatable['var_1_id_1'] = $('.item2').datatable();
datatable['var_1_id_2'] = $('.item3').datatable();
// later in the code.
var varname = 'var_1_id'+'_0';
console.log(datatable[varname]);
Try it like this:
var varname = 'var_1_id'+'_0';
alert(window[varname]);

Help needed with Javascript Objects

I need some help with Javascript objects. This is what I've got:
_genre_id = {politics:'1',sport:'2',celeb:'3',gossip:'4',busi:'5'};
var genre = 'politics';
What I want to accomplish would be to simply get the value stored for politics, in this case 1 by doing something like this:
var genreID = _genre_id.genre;
But this of course doesn't work because the genre property doesn't exist. I want it to relate to _genre_id.politics.
Any ideas would be welcome.
How about _genre_id[genre]?
var _genre_id = {politics:'1',sport:'2',celeb:'3',gossip:'4',busi:'5'};
var genre = 'politics';
var genreID = _genre_id[genre];
Note that I added var before setting _genre_id, so that it gets declared in the current scope, without declaring it, you end up setting window._genre_id instead of a local variable.

Categories

Resources