Domanda

Im trying to send the values from 2 hidden input fields on my form to 2 custom dimensions created inside google analytics, but I don't know how to send these when the form is submitted. Im using Gravity Forms also.
The dimensions created in google analytics are contactID and locationID:

My code looks like this:

<input name='input_4' id='input_6_4' type='hidden' class='gform_hidden' value='123456' />
<input name='input_25' id='input_6_25' type='hidden' class='gform_hidden' value='987654' />

jQuery(document).ready(function(){
  jQuery('#gform_6').submit(function(e) {
    var form = this;
    var contactID = $('#input_6_4').val();
    var locationID = $('#input_6_25').val();

    e.preventDefault(); // disable the default submit action

    ga('set', 'contactID', contactID);
    ga('set','locationID', locationID);

    $(':input', this).attr('disabled', true); 

    setTimeout(function() {
        form.submit();
    }, 1000);
  });
});

Thanks for anyone's help.

È stato utile?

Soluzione

There are 2 things that you need to consider:

1) Custom Dimensions and Metrics need to be sent with an existing hit. That means setting the custom dimensions alone will not send the values to Google Analytics. After you set them you'll need to send some type of hit to GA, such as an event, pageview, etc. You can also set them at the time of a hit (as in the example below).

2) Custom Dimensions and Metrics are set by using the index value for the dimension or metric. So in your case you might have named the dimension contactID but you actually need to figure out the index for that dimension and set that. You can find this in the Web Interface where you created the dimension, you should see an index column. So for example, if contactID has an index value of 2 then you would actually need to set dimension2. This is covered on the developers site, so you should read Custom Dimensions & Metrics.

Putting this altogether: for example, if the dimension index for contactID is 1, and for locationID it is 2, and if you decide to send an event upon form submission to make sure these values get sent to Google Analytics,

then you would replace

ga('set', 'contactID', contactID);
ga('set','locationID', locationID);

with:

ga('send', 'event', 'category', 'action', {
    'dimension1': contactID,
    'dimension2': locationID
});

and replace 'category' and 'action' with whatever values you want for the event.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top