使用Ruby / Rails应用程序中的Google Analytics跟踪JSON调用

时间:2021-07-08 15:19:16

I searched a lot about this issue but I can't find anything good. I think that this is a common problem: you have a web app and build JSON APIs on top of your platform, so you can develop some widgets, mobile apps or third party applications.

我搜索了很多关于这个问题,但我找不到任何好的东西。我认为这是一个常见问题:您拥有一个Web应用程序并在您的平台上构建JSON API,因此您可以开发一些小部件,移动应用程序或第三方应用程序。

I know that there a are a lot of specific analytics services via API (like Mixpanel, Kissmetrics and many others) but I want to track all the JSON calls also via Google Analytics.

我知道有许多通过API的特定分析服务(如Mixpanel,Kissmetrics和许多其他),但我想通过Google Analytics跟踪所有JSON调用。

I found that the best method is to use the __utm.gif image but strangely I cannot find any plugin or gem to use this image. So I tried to build my own method without success (see the code below)... Someone can help?

我发现最好的方法是使用__utm.gif图像但奇怪的是我找不到任何插件或gem来使用这个图像。所以我试图建立自己的方法但没有成功(参见下面的代码)......有人可以帮忙吗?

def google_analytics_call(page_title)
      today = Time.now.to_i
      utma = cookies[:__utma].to_s
      utmb = cookies[:__utmb].to_s
      utmc = cookies[:__utmc].to_s
      utmz = cookies[:__utmz].to_s
      utma = (rand(8).to_s + "." + rand(10).to_s + "." + today.to_s + "." + today.to_s+ "." + today.to_s) unless cookies[:__utma]
      utmb = rand(8).to_s unless cookies[:__utmb]
      utmc = rand(8).to_s unless cookies[:__utmc]
      utmz = rand(8).to_s+ "." + today + ".2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)" unless cookies[:__utmz]

      Thread.new do
        params = {
          :utmac => GOOGLE_ANALYTICS,
          :utmcc => "__utma%3D"+utma+"%3B%2B"+"__utmb%3D"+utmb+"%3B%2B"+"__utmc%3D"+utmc+"%3B%2B"+"__utmz%3D"+utmz+"%3B%2B",
          :utmcn => "1",
          :utmcs => "-",
          :utmdt => page_title, #page title
          :utmhn => BASE_URL,
          :utmwv => 1,
          :utmt => "page",
          :utmr => request.referer
        }
        puts params

        http_get("www.google-analytics.com", "/__utm.gif", params)
      end
    end

2 个解决方案

#1


3  

i think you want to use the async tracker, which is part of google analytics. Here is an example of how I track clicks that fire an async AJAX event:

我想你想使用异步跟踪器,它是谷歌分析的一部分。以下是我如何跟踪触发异步AJAX事件的点击的示例:

$('.trackable').bind('click', function(event){
              var action = $(this).attr('action');
              var category = $(this).attr('category');
              _gaq.push(['_trackEvent', action, category]);
            });

The "_gaq" is the google asynchronous queue and can be added to any JS function you want to track metrics within. The function above allows you to add a class called "trackable" and track events when the user clicks on it. An example looks like this:

“_gaq”是谷歌异步队列,可以添加到您想要跟踪其中的指标的任何JS函数中。上面的函数允许您添加一个名为“trackable”的类,并在用户单击它时跟踪事件。示例如下所示:

<a category='FEATURED' action='Like This' class='trackable' href="http://MYSSERVER.com/foo.json" target='_blank'>track asynchronously.</a>

#2


3  

Not quick answer, but I think you should try Gabba gem

没有快速回答,但我认为你应该尝试Gabba gem

#1


3  

i think you want to use the async tracker, which is part of google analytics. Here is an example of how I track clicks that fire an async AJAX event:

我想你想使用异步跟踪器,它是谷歌分析的一部分。以下是我如何跟踪触发异步AJAX事件的点击的示例:

$('.trackable').bind('click', function(event){
              var action = $(this).attr('action');
              var category = $(this).attr('category');
              _gaq.push(['_trackEvent', action, category]);
            });

The "_gaq" is the google asynchronous queue and can be added to any JS function you want to track metrics within. The function above allows you to add a class called "trackable" and track events when the user clicks on it. An example looks like this:

“_gaq”是谷歌异步队列,可以添加到您想要跟踪其中的指标的任何JS函数中。上面的函数允许您添加一个名为“trackable”的类,并在用户单击它时跟踪事件。示例如下所示:

<a category='FEATURED' action='Like This' class='trackable' href="http://MYSSERVER.com/foo.json" target='_blank'>track asynchronously.</a>

#2


3  

Not quick answer, but I think you should try Gabba gem

没有快速回答,但我认为你应该尝试Gabba gem