将隐藏的输入值作为自定义维度发送到Google Analytics

时间:2013-07-14 02:31:27

标签: jquery google-analytics custom-attributes form-submit gravity-forms-plugin

我尝试将表单上2个隐藏输入字段的值发送到Google Analytics中创建的2个自定义维度,但我不知道在提交表单时如何发送这些值。我也使用Gravity Forms 在Google Analytics中创建的维度是contactID和locationID:

我的代码如下所示:

<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);
  });
});

感谢任何人的帮助。

1 个答案:

答案 0 :(得分:1)

您需要考虑两件事:

1)自定义维度和指标需要与现有匹配一起发送。这意味着仅设置自定义维度不会将值发送到Google Analytics。设置它们之后,您需要向GA发送某种类型的匹配,例如事件,网页浏览等。您还可以在命中时设置它们(如下例所示)。

2)使用维度或指标的索引值设置自定义维度和指标。因此,在您的情况下,您可能已将维度命名为contactID,但实际上您需要确定该维度的索引并进行设置。您可以在创建维度的Web Interface中找到此内容,您应该会看到索引列。因此,例如,如果contactID的索引值为2,那么您实际上需要设置 dimension2 。这在开发者网站上有介绍,因此您应该阅读Custom Dimensions & Metrics

完全放置:例如,如果contactID的维度索引为1,则对于locationID,它为2,如果您决定在表单提交时发送事件以确保这些值得到发送到Google Analytics,

然后你会替换

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

使用:

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

并将'category'和'action'替换为您想要的事件值。