模拟 Google 分析 API
Mocking Google Analytics API
我想创建一个使用来自 google 分析 API 的数据的 Web 应用程序。问题是,为了使用 google 分析,该应用程序需要在线并有用户访问它。
那么有没有办法模拟 google 分析 API,或者生成一些用于开发和测试目的的虚假数据(即它让我手动设置综合浏览量和其他东西)?
您可以创建一个 Universal Analytics 属性 并使用 Measurement Protocol 将数据手动发送到 Google Analytics。
The Google Analytics Measurement Protocol allows developers to make
HTTP requests to send raw user interaction data directly to Google
Analytics servers.
https://developers.google.com/analytics/devguides/collection/protocol/v1
您可以使用此工具将命中发送到您的 Google 分析 属性:
您可以使用一些在线 api 模拟程序来模拟 api。
喜欢:https://themockapis.in/
您可以定义自定义路径和自定义响应。
根据您的环境,您可以使用像 Jest 这样的库来模拟您的 API 调用并让这些调用 return 模拟数据。
我最近用 Jest 在一个 Typescript 项目上做了这个。您需要做的第一件事是自动模拟所有可能看起来像这样的 API 调用:
export const analyticsMock = {
Management: {
Profiles: {
insert: jest.fn(),
list: jest.fn()
},
ProfileFilterLinks: {
/*
*this is a mocked endpoint with a mock return living in a
different file to keep things clean
*/
insert: jest.fn().mockReturnValue(profileFilterInsertResponseMock),
list: jest.fn()
},
Goals: {
insert: jest.fn().mockReturnValue(goalInsertResponseMock)
},
Filters: {
insert: jest.fn().mockReturnValue({id: "33333333"})
}
}
}
这是模拟函数可能 return 取决于端点的示例:
export const profileListMock = {
"kind": "analytics#profiles",
items: [
{
accountId: "1123123",
id: "123123123",
name: "Testing 1 DI Goals"
},
{
accountId: "1123123",
id: "654654654",
name: "Testing 2 Arnold Goals"
}
]
}
您只需确保在您的测试文件中还包含如下内容:
beforeEach(() =>{
global.Analytics = analyticsMock as unknown as GoogleAppsScript.Analytics
})
这允许我告诉程序它需要将我的模拟 (analyticsMock
) 扩展到 global.Analytics
,这允许我的模拟端点在测试文件中工作。
我想创建一个使用来自 google 分析 API 的数据的 Web 应用程序。问题是,为了使用 google 分析,该应用程序需要在线并有用户访问它。
那么有没有办法模拟 google 分析 API,或者生成一些用于开发和测试目的的虚假数据(即它让我手动设置综合浏览量和其他东西)?
您可以创建一个 Universal Analytics 属性 并使用 Measurement Protocol 将数据手动发送到 Google Analytics。
The Google Analytics Measurement Protocol allows developers to make HTTP requests to send raw user interaction data directly to Google Analytics servers. https://developers.google.com/analytics/devguides/collection/protocol/v1
您可以使用此工具将命中发送到您的 Google 分析 属性:
您可以使用一些在线 api 模拟程序来模拟 api。 喜欢:https://themockapis.in/ 您可以定义自定义路径和自定义响应。
根据您的环境,您可以使用像 Jest 这样的库来模拟您的 API 调用并让这些调用 return 模拟数据。
我最近用 Jest 在一个 Typescript 项目上做了这个。您需要做的第一件事是自动模拟所有可能看起来像这样的 API 调用:
export const analyticsMock = {
Management: {
Profiles: {
insert: jest.fn(),
list: jest.fn()
},
ProfileFilterLinks: {
/*
*this is a mocked endpoint with a mock return living in a
different file to keep things clean
*/
insert: jest.fn().mockReturnValue(profileFilterInsertResponseMock),
list: jest.fn()
},
Goals: {
insert: jest.fn().mockReturnValue(goalInsertResponseMock)
},
Filters: {
insert: jest.fn().mockReturnValue({id: "33333333"})
}
}
}
这是模拟函数可能 return 取决于端点的示例:
export const profileListMock = {
"kind": "analytics#profiles",
items: [
{
accountId: "1123123",
id: "123123123",
name: "Testing 1 DI Goals"
},
{
accountId: "1123123",
id: "654654654",
name: "Testing 2 Arnold Goals"
}
]
}
您只需确保在您的测试文件中还包含如下内容:
beforeEach(() =>{
global.Analytics = analyticsMock as unknown as GoogleAppsScript.Analytics
})
这允许我告诉程序它需要将我的模拟 (analyticsMock
) 扩展到 global.Analytics
,这允许我的模拟端点在测试文件中工作。