javascript 如何为 ajax 表单提交设置 Google Analytics 目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9908972/
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 08:11:11  来源:igfitidea点击:

How to set up Google Analytics Goal for ajax form submissions

javascriptjquerygoogle-analytics

提问by mykisscool

I have a contact form that is submitted via ajax and upon a successful submission, a thank you/success message is displayed.

我有一个通过 ajax 提交的联系表单,成功提交后,会显示一条感谢/成功消息。

Additionally, I've set up a goal (Goal Completion URL) in my Google Analytics account for visits to a thank you page.

此外,我在我的 Google Analytics(分析)帐户中设置了一个目标(目标完成 URL)以访问感谢页面。

This page does not exist.

本页面不存在。

Any thoughts/suggestions on how I can set up tracking successful form submissions via this method?

关于如何通过此方法设置跟踪成功表单提交的任何想法/建议?

采纳答案by jk.

You can use virtual pageviews. For each step of the process, add a call to

您可以使用虚拟综合浏览量。对于流程的每一步,添加一个调用

_gaq.push(['_trackPageview', '/ajax-contactForm/PAGE-or-STEP-NAME.html']);

This will register as a pageview and can be used as a step in the goal.

这将注册为综合浏览量,并可用作目标的一个步骤。

See virtual pageviews in the GA docs.

请参阅GA 文档中的虚拟综合浏览量

Or, to set it up as an event goal as Eduardo suggested, see The New Google Analytics: Events Goals

或者,要将其设置为 Eduardo 建议的事件目标,请参阅新的 Google Analytics:事件目标

回答by johnsnails

This answer possibly needs to be updated for more recent versions of GA. I did the following to set up goals when the page is submitted via ajax.

对于更新的 GA 版本,此答案可能需要更新。当页面通过ajax提交时,我做了以下设置目标。

    $.ajax({
        type: "POST",
        url: "/some/page/that/does/not/have/ga/on/it.php",
        data: { formData:formData },
        success: function() {
            // Some success message to user.
            // Create a virtual page view that you can track in GA.
            ga('send', {
                'hitType' : 'pageview',
                'page' : '/contact-us-success' // Virtual page (aka, does not actually exist) that you can now track in GA Goals as a destination page.
            });
        }
    });

Then in GA -> Admin -> Goals -> New Goal

(1) Goal setup - Custom
(2) Goal description -> choose 'Destination'.
(3) Goal details -> Destination Equals to /contact-us-success

Hope this helps someone else.

然后在 GA -> 管理 -> 目标 -> 新目标

(1) 目标设置 - 自定义
(2) 目标描述 -> 选择“目的地”。
(3) 目标详情 -> 目的地等于 /contact-us-success

希望这对其他人有帮助。

回答by Mark

Here's an updated answer for 2019. Linking up your Analytics account to Google Tag Manager lets you track submissions on AJAX forms in Google Analytics by either tracking all form submissions or by setting an event listener to an element's visibility (i.e. your form's confirmation/thank you message). It requires use of the newer Global Site Tag (gtag.js)and Google Tag Manager.

这是 2019 年的更新答案。将您的 Analytics 帐户链接到 Google Tag Manager,您可以通过跟踪所有表单提交或通过将事件侦听器设置为元素的可见性(即表单的确认/谢谢)来跟踪 Google Analytics 中的 AJAX 表单提交信息)。它需要使用较新的Global Site Tag (gtag.js)Google Tag Manager

This tutorialdoes an outstanding job of explaining the process and walks through setting up a Google Analytics Goal on a form submitted via AJAX or where tracking a redirect/changed URL isn't an option.

本教程出色地解释了该过程,并演练了在通过 AJAX 提交的表单上设置 Google Analytics 目标,或者在无法跟踪重定向/更改的 URL 的情况下。