google-trends-api-client
Version:
An fully typed and easy to use client for the google trends api.
48 lines • 2.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRelatedQueries = void 0;
const UnexpectedCodePathError_1 = require("../utils/errors/UnexpectedCodePathError");
const defineCommonGoogleTrendsApiParametersValue_1 = require("../assertions/defineCommonGoogleTrendsApiParametersValue");
const getExplorationWidgets_1 = require("./getExplorationWidgets");
const getFromApiCall_1 = require("./getFromApiCall");
/**
* per google
* > Users searching for your term also searched for these queries.
* > You can sort by the following metrics:
* > - TOP
* > - The most popular search queries.
* > - Scoring is on a relative scale where a value of 100 is the most commonly searched query, 50 is a query searched half as often as the most popular query, and so on.
* > - RISING
* > - Queries with the biggest increase in search frequency since the last time period.
* > - Results marked "Breakout" had a tremendous increase, probably because these queries are new and had few (if any) prior searches.
*/
const getRelatedQueries = async (...args) => {
// get the exploration widgets for this request, which give us the request token and params to get the related queries data
const { widgets } = await (0, getExplorationWidgets_1.getExplorationWidgets)(...args);
// find the relevant widget
const relevantWidget = widgets.find((widget) => widget.id === 'RELATED_QUERIES');
if (!relevantWidget)
throw new UnexpectedCodePathError_1.UnexpectedCodePathError('could not find widget with id RELATED_QUERIES');
// make the the request for the widgets data
const response = await (0, getFromApiCall_1.getFromApiCall)({
path: 'trends/api/widgetdata/relatedsearches',
agentOptions: args[1],
queryParams: {
...(0, defineCommonGoogleTrendsApiParametersValue_1.defineCommonGoogleTrendsApiParametersValue)({
hl: args[0].hl,
tz: args[0].tz,
}),
req: JSON.stringify(relevantWidget.request),
token: relevantWidget.token,
},
});
const result = JSON.parse(response);
return {
top: result.default.rankedList[0]
.rankedKeyword,
rising: result.default.rankedList[1]
.rankedKeyword,
};
};
exports.getRelatedQueries = getRelatedQueries;
//# sourceMappingURL=getRelatedQueries.js.map