RESTful API和Google Analytics(分析) [英] RESTful API and Google Analytics

查看:110
本文介绍了RESTful API和Google Analytics(分析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python( Flask )运行 RESTful API .

I'm running an RESTful API with Python (Flask).

我希望能够跟踪:

  • 已发出哪些请求
  • 这些请求何时发生
  • 发送回复花了多长时间

我想为此使用 Google Analytics(分析),因为它具有很好的仪表板和扩展的功能.

I want to use Google Analytics for this, because of it's nice dashboard and extended functionalities.

如何将Google Analytics(分析)实现为REST API?
还是有人知道可以实施的其他工具/库?

How can I implement Google Analytics into a REST API?
OR does anyone know another tool/library that can be implemented?

这是我目前所发现的:

  • a tracking app that uses MongoDB
  • the Google data API - but this is for reading GA data, not tracking the API?

推荐答案

实际上,有两种方法可以将服务器端数据发送到Google Analytics(分析).标准方法是GIF图片请求API,它与ga.js在客户端使用的API相同. Google已开始开发一种称为Measurement Protocol的新REST API,但这仅在开发人员预览版中.

There are actually two ways to send server-side data to Google Analytics. The standard method is the GIF Image Request API, which is the same API that ga.js uses on the client-side. Google has started developing a new REST API known as the Measurement Protocol, but this is only in developer preview.

服务器端GA

尝试将服务器端数据发送到GA时需要解决一些问题.

There are a few issues to work through when trying to send server-side data to GA.

就像上面提到的@mehaase一样,gif API从请求中获取IP地址,因此所有服务器端请求都将以用户身份出现在服务器位置.测量协议也不允许您更改请求的IP .我将在此答案中使用公开可用的gif API.

Like @mehaase pointed out above, the gif API takes the ip address from the request, so all of your server-side requests will appear as users coming from the location of your servers. The measurement protocol doesn't let you change the request's ip either. I'll assume the publicly available gif API in this answer.

另一个问题是gif端点需要客户端cookie.您可以在每个请求上伪造此Cookie,但这会使每个事件看起来像一个新访客.只要将服务器端API和网站保留在单独的Google Analytics(分析)配置文件中,就可以了.

Another issue is that the gif endpoint requires a client-side cookie. You can fake this cookie on every request but this will cause each event to look like a new visitor. That's fine as long as you keep the server-side API and website in separate Google Analytics profiles.

还请注意,一旦您发送事件,Google最多可能需要一个小时才能显示您的事件.这会使调试有些痛苦,请耐心等待.

Also beware that Google can take up to an hour to show your events once you've sent them. This can make debugging a bit painful, so be patient.

以下是 GA中每个变量的分类Cookie的意思是,还有一个好的node.js将服务器端数据发送到GA的示例.

其他事件跟踪选项

尽管GA非常适合跟踪网站指标,但它并不是为跟踪服务器端事件而构建的.一类称为事件跟踪的分析方法是用于静态API使用情况跟踪的完美应用程序.

Even though GA is excellent for tracking website metrics, it's not built for tracking server-side events. A category of analytics known as event tracking is the perfect application for restful API usage tracking.

API通常如下所示:

The API generally looks like this:

analytics.track('API Response', {
    method  : 'POST',
    endpoint: '/comments'
    duration: 124
    status  : 500
});

并允许您查看有关每个事件和事件属性的频率和分布的报告.您可以回答以下问题:今天发生了多少个/comment API调用?几百个?有多少个响应高于200毫秒?等

And lets you see reports on the frequencies and distributions of each event and event property You can answer questions like: how many /comments API calls happened today? How many were 200s? How many had a response higher than 200ms? etc.

以下一些事件跟踪工具可以帮助您做到这一点:

Here are some event tracking tools that can help you do this:

  1. 混合面板
  2. KissMetrics
  3. Keen.IO
  1. Mixpanel
  2. KissMetrics
  3. Keen.IO

我是 Segment.io 的联合创始人,该公司为客户端提供了简单的API,服务器端和移动分析.我们让您从python,php,ruby,node,java,.net,javascript和iOS发送数据,然后将其转发到Google Analytics(分析),Mixpanel,KissMetrics,Keen.IO或任何其他受支持的服务,而无需您必须学习他们的API.

I'm the co-founder of Segment.io, a company that provides a simple API for client-side, server-side and mobile analytics. We let you send data from python, php, ruby, node, java, .net, javascript, and iOS, and we'll forward it to Google Analytics, Mixpanel, KissMetrics, Keen.IO, or any of our other supported services without you having to learn their API.

最后,这是我们的分析学院上的一篇文章,为什么事件跟踪很有用.

And finally, here's an article from our analytics academy that explains why event tracking is useful.

这篇关于RESTful API和Google Analytics(分析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆