代码之家  ›  专栏  ›  技术社区  ›  wnvko

通过节点http发布到Google Analytics

  •  0
  • wnvko  · 技术社区  · 6 年前

    我正在尝试使用simple在节点下发布到Google Analytics http 请求如下:

    var http = require("http");
    var post_options = ({
        host: "www.google-analytics.com",
        path: "/collect",
        body: "ec=a&ea=bb&ev=1&cid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&v=1&tid=UA-123456789-0&t=event",
        method: "POST",
        headers: {}
    });
    http.request(post_options, function (res) {
        console.log(res);
    }).end();
    

    但这从来没有到达谷歌分析服务器,我在那里的统计数据中看不到它。使用 https 这样也不行:

    var http = require("http");
    var post_options = ({
        hostname: "www.google-analytics.com",
        port: 443,
        path: "/collect",
        body: "ec=a&ea=bb&ev=1&cid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&v=1&tid=UA-123456789-0&t=event",
        method: "POST",
        headers: {}
    });
    https.request(post_options, function (res) {
        console.log(res);
    }).end();
    

    如果我用 request 模块,如下所示:

    var request = require("request");
    var path = "https://www.google-analytics.com/collect";
    var options = { "body": "ec=a&ea=bb&ev=1&cid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&v=1&tid=UA-123456789-0&t=event", "headers": {} }
    request.post(path, options, (err, httpResponse, body) => {
        console.log(httpResponse);
    });
    

    它立即显示在谷歌分析服务器上。你知道我的第一种方法有什么问题吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   ralphtheninja    6 年前

    第一种方法使用 http 核心模块 POST 请求收件人 http://www.google-analytics.com/collect 。切换到 https 而不是核心模块。