How to pass variables from a hidden field to Google Analytics


#1

Hi there!

I’m trying to piece together some JavaScript I found on various threads to achieve the following:

  1. User lands on LP
  2. A unique value is created for the user in a form hidden field
  3. Upon the user successfully submitting the form, the hidden value is then pushed to analytics as a transaction ID

The goal is to create a unique ID for each lead that can then be sent to analytics, as well as any other integration we may have.

So far here is what I have found:

To create the unique ID and place it in a hidden form field

<script type="text/javascript">   
$(document).ready(function() {
    var id = new Date().toISOString().replace(/[-tTzZ:.]/g, '');
    $('#lead_id').val(id);
    });
</script>

Trying to use an event trigger to send a transaction ID to analytics

<script type="text/javascript">  
 $('.lp-pom-form').on('submit', function() { 
    ga('ecommerce:addTransaction', {
      'id': '1234'
    });
</script>

How do I get the value that the first script generates to also go where where it is says 1234 in the next script?

As I’m a novice here, any help is gladly appreciated.

:grinning: