Assign cookie value to a hidden field


#1

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!