javascript 如何设置没有单独感谢页面的联系表单的 Google Analytics 目标跟踪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8308723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:00:05  来源:igfitidea点击:

How to set up Google Analytics goal tracking of a contact form with no separate thank you page?

javascriptgoogle-analyticsonsubmitgoal-tracking

提问by RSM

How do I set Google Analytics up to track visitors to my website who have submitted a contact form, that doesn't have a separate thank-you url?

如何设置 Google Analytics(分析)以跟踪已提交联系表单但没有单独感谢 URL 的网站访问者?

Ive seen the code posted around blogs and GA help forums but so far have yet to come across how would I actually set the goal up in order to insert the snippet of code.

我已经看到在博客和 GA 帮助论坛上发布的代码,但到目前为止还没有遇到我将如何实际设置目标以插入代码片段。

Code found on forums and such:

在论坛上找到的代码等:

onsubmit="pageTracker._trackPageview('/Goa1-Button'); pageTracker._trackEvent('Goals','CLick-Button');"

Specifically, I would like to know things such as:

具体来说,我想知道诸如:

  • What goal type would I use?
  • If i named the campaign Contact form completions, where would it go and fit in to the code above?
  • Is the code above right and will it help me?
  • 我会使用什么目标类型?
  • 如果我将营销活动联系表单命名为完成,它会去哪里并适合上面的代码?
  • 上面的代码是否正确,对我有帮助吗?

Any other advice, any one ever had to do this before?

任何其他建议,任何人以前都必须这样做吗?

回答by Crayon Violent

So it looks like from the URL you posted in a comment on yahelc's answer, that you have a form that submits and gives back a response via AJAX.

因此,从您在 yahelc 答案的评论中发布的 URL 看来,您有一个通过 AJAX 提交并返回响应的表单。

Also, your on-page GA code is the async version, but the code you have in your question is the traditional, so you need to use the async syntax.

此外,您的页面 GA 代码是异步版本,但您问题中的代码是传统的,因此您需要使用异步语法。

On your page, if the visitor does not fill something out, the area in question gets highlighted in red (sidenote: I see no "you need to fill this out" or "this is the correct format" messages if I do not fill out the form correctly..you should look into adding that...).

在您的页面上,如果访问者未填写某些内容,则相关区域将以红色突出显示(旁注:如果我不填写,我将看不到“您需要填写此内容”或“这是正确格式”的消息表格正确......你应该考虑添加......)。

The main thing you need to make sure is that you only pop the 'success' code if the visitor successfully fills out the form. So you don't really want to attach the GA code to the onsubmit because this can produce false positives..it will trigger whenever the visitor clicks the submit button, regardless of whether or not they successfully filled out the form.

您需要确保的主要事情是只有在访问者成功填写表格时才弹出“成功”代码。因此,您真的不想将 GA 代码附加到 onsubmit 中,因为这会产生误报……只要访问者单击提交按钮,它就会触发,无论他们是否成功填写了表单。

So it looks like the javascript that handles form validation is in your /custom.js, and you have on line 163 of custom.js viewsource the following:

所以看起来处理表单验证的javascript在你的/custom.js中,你在custom.js视图源的第163行有以下内容:

success: function(response){

                           jQuery(".ajax_form").before("<div class='ajaxresponse' style='display: none;'></div>");

                           jQuery(".ajaxresponse").html(response).slideDown(400);

                           jQuery(".ajax_form #send").fadeIn(400);

                           jQuery(".ajax_form input, .ajax_form textarea, .ajax_form radio, .ajax_form select").val("");

                               }

                            });

This looks to be where the "thank you" message is displayed, after the form has been validated and submitted, so you should put your GA "success" code somewhere in this function.

在验证并提交表单后,这看起来是显示“谢谢”消息的地方,因此您应该将 GA“成功”代码放在此函数中的某个位置。

The code you will want to insert should look something like this (based on the code in your question):

您要插入的代码应如下所示(基于您问题中的代码):

_gaq.push(['_trackEvent', 'Goals', 'CLick-Button']);
_gaq.push(['_trackPageview','/Goa1-Button']);

NOTE: For the event tracking, this will set the event category to "Goals" and the event action to "CLick-Button". There are other optional arguments you can pass to the _trackEvent for further granularity. Refer to GA's event tracker guidefor more details.

注意:对于事件跟踪,这会将事件类别设置为“目标”,将事件操作设置为“单击按钮”。您可以将其他可选参数传递给 _trackEvent 以获得进一步的粒度。有关更多详细信息,请参阅 GA 的事件跟踪器指南

As for the goal tracking, as yahelc mentioned, this is setup within the interface. The code above will send a virtual page view with a page name of "/Goa1-Button" and you will use this value in setting up your goal. There are a lot of ways you can set it up the goal. You can make it exactly match that value or start with that value if you anticipate URL params being added to it later, etc...(but also note, you cannot currently create goals based on events...which is lame, but I hear GA is working on making that happen eventually).

至于目标跟踪,正如 yahelc 提到的,这是在界面中设置的。上面的代码将发送一个页面名称为“/Goa1-Button”的虚拟页面视图,您将在设置目标时使用此值。有很多方法可以设置目标。如果您预计稍后会添加 URL 参数,您可以使其与该值完全匹配或从该值开始,等等...(但请注意,您目前无法根据事件创建目标...这是蹩脚的,但我听说 GA 正在努力最终实现这一目标)。

edit: Apparently you actually can set goals based on events, if you use the "New Version", as mentioned by yahelc in his answer comments. Nice!

编辑:显然,如果您使用 yahelc 在他的回答评论中提到的“新版本”,您实际上可以根据事件设置目标。好的!

回答by Yahel

Goals are configured from within the Google Analytics interface, and do not apply retroactively.

目标是在 Google Analytics(分析)界面内配置的,不会追溯应用。

You should check out How do I set up goals and funnels?

您应该查看如何设置目标和渠道?

You can specify a specific page, event or amount of time on site as your goal.

您可以将特定页面、事件或网站停留时间指定为您的目标。

As far as how to configure the code that will track submission of your form, that requires more information (i.e. code samples) to help you with. But, most importantly: Is it an AJAX form, or a regular form that just posts to the same URL? Are you using async or traditional Google Analytics syntax?

至于如何配置跟踪表单提交的代码,这需要更多信息(即代码示例)来帮助您。但是,最重要的是:它是 AJAX 表单,还是仅发布到同一 URL 的常规表单?您使用的是异步还是传统的 Google Analytics 语法?

EDIT:

编辑:

Based on the form you just posted, it looks like its an AJAX POST that returns an HTML body.

根据您刚刚发布的表单,它看起来像是一个返回 HTML 正文的 AJAX POST。

So, all you need to do is add your "goal" code into that markup, something like:

因此,您需要做的就是将“目标”代码添加到该标记中,例如:

<script>
_gaq.push(["_trackPageview", "/contact-us"]); //for a URL goal
_gaq.push(["_trackEvent", "Contact Us", "Submit"]); //for an event goal.
</script>