gpt4 book ai didi

node.js - 使用 Node http 发布到 Google Analytics

转载 作者:行者123 更新时间:2023-12-02 06:37:35 26 4
gpt4 key购买 nike

我正在尝试使用简单的 http 请求在 Node 下发布到 Google Analytics,如下所示:

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();

但这永远不会到达 Google Analytics 服务器,而且我无法在统计数据中看到它。像这样使用 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);
});

它会立即显示在 Google Analytics 服务器中。知道我的第一种方法有什么问题吗?

最佳答案

第一种方法使用 http 核心模块,因此它将向 http://www.google-analytics.com/collect< 发出 POST 请求。切换到 https 核心模块。

关于node.js - 使用 Node http 发布到 Google Analytics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49266471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com