如何使用Google Analytics跟踪AJAX网站搜索?

时间:2022-01-15 14:01:58

I'm using AJAX (POST method) for my site search. Here's my AJAX call:

我正在使用AJAX(POST方法)进行网站搜索。这是我的AJAX调用:

$.ajax({
    url: 'ajax.php',
    type: 'POST',
    dataType: 'json',
    data: {
        search_phrase: search_phrase
    },
    success: function(data) { ...

My aim is to use Google Analytics to start tracking search terms. Any idea how this can be done?

我的目标是使用Google Analytics开始跟踪搜索字词。知道如何做到这一点?

2 个解决方案

#1


3  

In your success callback lodge a virtual pageview with Google Analytics. Overwrite the page page for that pageview to include a query parameter (unlike KayKay's answer I'm assuming Universal Analytics):

在您的成功回调中,使用Google Analytics提供虚拟综合浏览量。覆盖该综合浏览量的页面以包含查询参数(与KayKay的答案不同,我假设是Universal Analytics):

success: function(data) { 
  ga('send', 'pageview', 'search.php?q=mykeyword');
  .........

(there is not enough info about yor page code to see how to retrieve the keyword - either read it with jQuery from your search input field, or you can extract it from the ajax request).

(关于yor页面代码的信息不足以查看如何检索关键字 - 从搜索输入字段中使用jQuery读取它,或者您可以从ajax请求中提取它)。

This will count every search as a pageview, but since searching replaces the content that makes sense IMO.

这会将每次搜索都计为页面浏览量,但由于搜索会替换有意义的IMO内容。

Set up internal site search in your views to work with (in my example) the "q" parameter (and click "remove search parameter from url").

在视图中设置内部站点搜索以使用(在我的示例中)“q”参数(并单击“从url中删除搜索参数”)。

If you do not want to touch your ajax function you can use global ajax event handlers from jQuery, but that's a bit harder to set up.

如果你不想触摸你的ajax函数你可以使用jQuery中的全局ajax事件处理程序,但这有点难以设置。

#2


1  

You can trigger a google analytics event when sendind the ajax call. This is done as shown in the documentation using :

您可以在sendind ajax调用时触发Google Analytics事件。这是使用以下文档中所示完成的:

_gaq.push(['_trackEvent', 'YourAjaxCallName', 'The posted value as a string']);

#1


3  

In your success callback lodge a virtual pageview with Google Analytics. Overwrite the page page for that pageview to include a query parameter (unlike KayKay's answer I'm assuming Universal Analytics):

在您的成功回调中,使用Google Analytics提供虚拟综合浏览量。覆盖该综合浏览量的页面以包含查询参数(与KayKay的答案不同,我假设是Universal Analytics):

success: function(data) { 
  ga('send', 'pageview', 'search.php?q=mykeyword');
  .........

(there is not enough info about yor page code to see how to retrieve the keyword - either read it with jQuery from your search input field, or you can extract it from the ajax request).

(关于yor页面代码的信息不足以查看如何检索关键字 - 从搜索输入字段中使用jQuery读取它,或者您可以从ajax请求中提取它)。

This will count every search as a pageview, but since searching replaces the content that makes sense IMO.

这会将每次搜索都计为页面浏览量,但由于搜索会替换有意义的IMO内容。

Set up internal site search in your views to work with (in my example) the "q" parameter (and click "remove search parameter from url").

在视图中设置内部站点搜索以使用(在我的示例中)“q”参数(并单击“从url中删除搜索参数”)。

If you do not want to touch your ajax function you can use global ajax event handlers from jQuery, but that's a bit harder to set up.

如果你不想触摸你的ajax函数你可以使用jQuery中的全局ajax事件处理程序,但这有点难以设置。

#2


1  

You can trigger a google analytics event when sendind the ajax call. This is done as shown in the documentation using :

您可以在sendind ajax调用时触发Google Analytics事件。这是使用以下文档中所示完成的:

_gaq.push(['_trackEvent', 'YourAjaxCallName', 'The posted value as a string']);