Expand my Community achievements bar.

SOLVED

Profile Script return numerical value

Avatar

Level 2

Hi,

I've created a profile script which is taking an mbox parameter for 'Sign up date' (which is a text string: "yyyy-mm-dd"), and converting it into a number, whose digits are YYYYMMDD. The value returned in mboxTrace for my test account is ''2.0170713E7" (13th July 2017).

My hope is to then create an audience with criteria like "user.param IS GREATER THAN 20170728", or any others as and when they are needed. I.e., this is saying 'only target visitors who signed up after 28th July 2017'.

After testing, it doesn't appear to be working as expected. In some cases my content shows correctly, and others it doesn't, but it doesn't seem to correlate with numerical magnitude.

Is the 'greater than' criteria a sensible thing to use for user parameters, or are they always returned as text strings? If so, does anyone have an idea for an alternative solution?

Many thanks,

Chris

1 Accepted Solution

Avatar

Correct answer by
Level 4

Your code looks fine.  So I would take a look at the data that is passed in your mbox.param('registrationdate') and validate that its always returning a date that you can use.  Since its a string, if there are any letters or other characters in there, it could be causing an error.  If the first character cannot be converted to a number, parseInt() returns NaN.

First you could try and run a regex to remove all non digits from the string before converting it to a number.  Something like:

var dateText = year + month + day;

dateText = dateText.replace(/\D/g,'');

var date = parseInt(dateText);

Second idea, would be to do the date comparison within the profile script and return true if its greater than.  Basically convert the string to a date and then do the comparison in JS.  The limitation here would be that you would have a hard coded date and not a dynamic date that you could change for different campaigns within your test setup.

View solution in original post

4 Replies

Avatar

Level 4

Can you post your full profile script code? Or if its long, at least the portion that the creates the date and converts the string to a number?  And also, in your value "2.0170713E7" there is a period and "E7".  Are these typos?  What is the E7?  I would think this is a string and not a number and therefore the greater than behaves differently with strings.

Avatar

Level 2

Sure, script below:

if (mbox.name == 'dashboard_header_banner') {

      var signUpDate = mbox.param('registration_date'); //"yyyy-mm-dd"

      var day = signUpDate.split("-")[2];

      var month = signUpDate.split("-")[1];

      var year = signUpDate.split("-")[0];

      var dateText = year + month + day;

      var date = parseInt(dateText);

      return date

}

The 'E7' just means x10^7. It's shorthand; the date format I've used yields a number over 20 million, and it doesn't like having that many digits. It's still technically a number.

I did think it might be returning a string, though. Thought that it was possible to use numbers though, as the 'greater than' option is available to use.

Avatar

Correct answer by
Level 4

Your code looks fine.  So I would take a look at the data that is passed in your mbox.param('registrationdate') and validate that its always returning a date that you can use.  Since its a string, if there are any letters or other characters in there, it could be causing an error.  If the first character cannot be converted to a number, parseInt() returns NaN.

First you could try and run a regex to remove all non digits from the string before converting it to a number.  Something like:

var dateText = year + month + day;

dateText = dateText.replace(/\D/g,'');

var date = parseInt(dateText);

Second idea, would be to do the date comparison within the profile script and return true if its greater than.  Basically convert the string to a date and then do the comparison in JS.  The limitation here would be that you would have a hard coded date and not a dynamic date that you could change for different campaigns within your test setup.

Avatar

Level 2

Thanks very much, it appears to work fine now.

Chris