kand
kand

Reputation: 2338

Tracking Submitted Form Values with Google Tag Manager and Google Analytics

I am using Google Tag Manager to handle Google Analytics. There is a form on my website that has a submit button which I've successfully set up a click listener for through Google Tag Manager.

With this click listener I would like to track what input values where present in the form at the time the click listener fires, then be able to sort events in Google Analytics based on these values. I've looked into Google Analytics Dimensions and Metrics but these don't seem to be able to store form values the way I would like them to.

Does anyone know the best way to go about this? I think I might be having trouble understanding what Dimensions and Metrics are really supposed to be used for... Is there some other Google Analytics tool that would accomplish more easily?

Upvotes: 3

Views: 2034

Answers (1)

Nick Blexrud
Nick Blexrud

Reputation: 9603

Take a look at this discussion on Google Tag Manager Community.

Here is the just of it:

  1. Create a new Custom JavaScript Macro
  2. Assuming you're using auto events:

    function() {
      // Assuming that {{element}} is the form you want.
      // This should be the case if you're using Auto Events.
      var form = {{element}};
    
      // Assuming there's only one <select> in the form.
      var select = form.getElementsByTagName('select')[0];
    
      var results = [];
      for (var i=0; i<select.options.length; i++) {
    
        // Assuming you want the value attributes of the selected.
        // You could also use .text here instead of .value.
        if (select.options[i].selected) results.push(select.options[i].value);
      }
    
      // Assuming you want an array of the selected values.
      // If you want a CSV string, return results.join() instead.
      return results;
    }
    

All this code was written by Brian Kuhn via the Google Plus post I reference above. Not trying to take credit.

Upvotes: 3

Related Questions