javascript - 将 G Analytics 集成到 Node.js API

标签 javascript node.js rest google-analytics

我目前正忙于这个项目:将 GA 集成到我的 REST API,以便我可以检索指标和维度。 我必须强调,我正在本地主机上运行 API,这可以解释为什么它不起作用......?

我尝试按照不同的方式集成 GA:

1. Using universal-analytics (https://github.com/peaksandpies/universal-analytics/blob/master/AcceptableParams.md)

const ua = require('universal-analytics');
require('dotenv').config({path: '../.env'});

// to retrieve the number of connexions to the homepage
server.route({
    method: 'GET',
    path: '/homepage',
    handler: function (request, reply) {
       ...
       const visitor = ua(process.env.UA_ID);
       visitor.pageview('/bookmarks', 'http://localhost:4000', 'Homepage', (err) => {
          if (err) {
             throw err;
           }
       });

      return reply.view('index');
    }
 });

 // I have pretty much the same thing at the end of a POST endpoint handler
 // to retrieve the number of new data posted

   handler: function (request, reply) {
   ...
   const user = ua(process.env.UA_ID, userId(request.auth.credentials.username));
   user.event('Bookmarks', 'new_bkm_created', payload.title, (err) => {
       if (err) {
          throw err;
        }
    });
 }

2. Creating a trackEvent function (https://cloud.google.com/appengine/docs/flexible/nodejs/integrating-with-analytics)

在 google.js 中

const got = require('got');

module.exports = function trackEvent (propertyId, category, action, options) {
  const data = {
      v: '1',   // API version
      tid: propertyId,   // Tracking / Property ID.
      t: 'event',   // Event hit type.
      ec: category,  // Event category.
      ea: action   // Event action.
  };

  if (options) {
      if (options.label) {
        data.el = options.label; // Event label.
      }
      if (options.value) {
        data.ev = options.value; // Event value.
      }
  }

  return got.post('http://www.google-analytics.com/collect', {
      body: data,
      form: true
  });
};

在 app.js 中

require('dotenv').config({path: '../.env'});
const trackEvent = require('./google');

const postHandler = (request, reply) => {
  ...
  trackEvent(process.env.UA_ID, 'Thing', 'new_thing_created').then(() => {
    return reply(bookmark).code(201);
  }
} 

3. Pasting the website tracking code from Admin --> Property ---> Tracking Info (https://developers.google.com/analytics/devguides/collection/analyticsjs/)

<script>
(function(i,s,o,g,r,a,m)i['GoogleAnalyticsObject']=ri[r]=i[r]||function(){  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com, 'analytics.js','ga');
ga('create', 'UA-XXX-whatev', 'auto');
ga('send', 'pageview');

</script>

我还尝试了第三个标题的链接上建议的异步版本。问题是,我不喜欢这种做法,我不明白如何从前端创建的 ga 的处理程序中启动事件和页面 View ...

无论如何,它们都不起作用...我没有错误,但 G Analytics 中没有任何显示。 如您所知,我在默认 URL 中使用了 example.com,因为它不适用于本地主机 URL。

你有什么线索可以帮助我解决这个问题吗? 提前非常感谢您!

-- smgr

最佳答案

事实证明,三种方法中的一种有效:第一种方法采用通用分析。人们需要耐心等待才能看到第一个跟踪的事件。不过,我强烈推荐这个模块,它对开发人员非常友好;)

关于javascript - 将 G Analytics 集成到 Node.js API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45172790/

相关文章:

javascript - nodejs mongodb驱动的同步函数调用

node.js - 启动 N|Solid Proxy 时出现 'Could not find configuration for port' 错误

asp.net-mvc - ASP.NET MVC中类似OData的路由

java - Swagger Spring-MVC/SwaggerUI 身份验证

javascript - 向下滚动到 div,同时隐藏第一个 div,使页面向下跳转

javascript - 更改页面时AngularJS自定义窗口

javascript - 返回包含另一个数组中的阶乘数的数组

javascript - Node 表达 : not able to access data after query execution

node.js - 如何使用 Sockets.io + Node.js 服务器获取客户端 IPv4 地址(而非 IPv6)

json - 使用 Spring MVC RESTful Web 服务的 Angularjs