Assign cookie value to a hidden field

  • 20 May 2018
  • 2 replies
  • 130 views

Hey guys, I have to offer you a nice puzzle (notice: long post).

I’m making a landing page for the sale of a new service. The page is traced through Google tag Manager. The page will be communicated through an email campaign to a list of existing customers, each of which is uniquely identified in a mysql db (pk: “id”, integer 11, autoincrement, not null).

The email will contain a link like this: https://mypage.com?ddmid=[id]#1. When the Customer clicks on the link, GTM does the following:

  • assign the id value to a data layer variable. At the same time, it creates a first party cookie that stores the value on it
  • then, through a custom javascript function, check if the id is contained in the URL. If this is not the case, check if the id is contained in the cookie.
    In both cases, the id is communicated to GA through a tag, so you can use the userId reports: this is useful to identify the customer even if he arrives on the page a second time, perhaps by directly typing the url on the browser, without clicking on the original email link.

When the Customer fills out the form, the value of the id is returned to a hidden field. This is simple if the form is filled out on the first visit, ie when the id is contained in the URL. And here is my question.

If the customer completes the form on the second visit, how can I retrieve the value of the id contained in the cookie and assign it to the hidden field? Is better to do it directly on page or through GTM?

Know that I only have basic knowledge about javascript. Thanks to everyone who will have a cut!


2 replies

did you manage to store the form fields ?

I use a bit of jQuery to assign cookie values to hidden fields with the code below. Note, be sure you are loading jQuery first.

<script>
  $(document).ready(function() {
   	$( "#field_id1" ).val(getCookie("YourCookiedName1"));
   	$( "#field_id2" ).val(getCookie("YourCookiedName2"));
   	$( "#field_id3" ).val(getCookie("YourCookiedName3"));
   	$( "#field_id4" ).val(getCookie("YourCookiedName4"));
   	$( "#field_id5" ).val(getCookie("YourCookiedName5"));
   });
 </script>

Reply