Add a value to a multi-select fields from tracking code

Hi, I want to add some values to a custom multi-select field from the tracking code. These are details which I can collect from the interests and behaviour of the contact in my website.



I can set a single value doing like this:



mt(‘send’, ‘pageview’, {email: "email@domain.com", flags: “value1”});



However, if later I do the same with another value, it replaces the first one, instead of adding to it.



Is it possible to add (and remove) values from a multi-select field using the Javascript tracking code?



Regards,

Xavier

1 Like

Hi, I want to add some values to a custom multi-select field from the tracking code. These are details which I can collect from the interests and behaviour of the contact in my website.

I can set a single value doing like this:

mt(‘send’, ‘pageview’, {email: "email@domain.com", flags: “value1”});

However, if later I do the same with another value, it replaces the first one, instead of adding to it.

Is it possible to add (and remove) values from a multi-select field using the Javascript tracking code?

Regards,
Xavier

1 Like

hey, stuck in the same problem. Any solutions to this?

according with the documentation, you can use the tag field:

mt('send', 'pageview' , { email: 'example@domain.com', tags: 'addThisTag,-removeThisTag' })

I don’t think other types of field would work this way, what you could try is retrieve the existing values from someplace* then adding all the values again.

  • here are some options:
  1. save the data in a cookie / LocalStorage and everytime you would change that value (either adding or removing values) you would call the mt function with the new sets of values again, this could be an issue if the same contact uses different devices or clears the cookies

  2. retrieve the value of the field from mautic , and use the existing value to compose the new value before calling the mt function.
    the data will be more reliable but you will add additional http requests.
    you can create a mautic form with just hidden fields already populated with the existing values from the contact.then use those values to populate the mt function

  3. use the mt_id cookie that the tracking saves in your domain (1st party cookie) - this cookie contains the contact id, you can update the contact values thru the REST API (do this server side)

Usually using Tag values should be the best option in general, for the other options, be sure you update the flags field (andretrieve data from mautic) just when necessary, not in every pageview.

1 Like

Thanks a lot for your quick reply.
Yes, tag perfectly fits into the use case.
For retrieving data, I have to look into APIs which and will try and let you know.
I was about to use tags but actually I wanted multiple fields like this, say 1 such field for the merchant, and one such field for the product. Now I have to add both of them in tag only.
I will try this retrieving data method tho.
Thanks a lot.
Also, 3 quick questions.

  1. can you help me with the part where i retrieve data? I understood the 1st point, but I didn’t understand mautic form with hidden fields and the REST API one.
  2. do you by any chance have any idea regarding lookup fields?
  3. are there other mt events that I can use apart from pageview?

and I still think, even if I fetch the value, I won’t be able to add new value/option in the custom field

here:

I will still only be able to select either 1, or both of these value, nothing new

Option for using a form (notice that I have not implemented it, its just that I think should work in theory)

  1. create a form in mautic (Under Components → Form → Stand Alone )
  2. Add a field Type Hidden, add the label you want
  3. on the Contact Field Tab, select the field you created test_select_multiple
  4. on the behavior tab change “Auto fill data” to Yes

so basically you have created a form that contains a hidden field containing the value of the test_select_multiple already populated for the contact being tracked, so you can read that field using javascript, concatenate with the new value and then populate the mt(‘send’) method

using the API, you have to call the API server side, so instead of including the value of the field in the mt(send) you would have to trigger the update in another way, if you requesting an php page, for example, the API call could be done in that php request before sending the html to the page
the mautic id of the user can be obtained by reading the cookie named mtc_id
for details of the api call:
https://developer.mautic.org/#edit-contact

also, keep in mind that customer interesting and behaviours are usually tracked by the page view event itself, does the user visit a product category page? eg; (store/category/computers/) what the user search for? (url would be something like search?query=myinteresting)
all of those page views will be recorded in mautic, and you can create segments based on those page views. (customers who visits specific urls) and create campaigns for those segments