in marketing-analytics/activation/common-libs/nodejs-common/src/apis/measurement_protocol.js [63:117]
getSinglePingFn(config) {
/**
* Sends a batch of hits to Measurement Protocol.
* @param {!Array<string>} lines Data for single request. It should be
* guaranteed that it doesn't exceed quota limitation.
* @param {string} batchId The tag for log.
* @return {!Promise<BatchResult>}
*/
return async (lines, batchId) => {
const payload = lines.map((line) => {
const record = JSON.parse(line);
const hit = Object.assign({}, config, record);
return Object.keys(hit).map(
(key) => `${key}=${encodeURIComponent(hit[key])}`)
.join('&');
})
.join('\n');
// In debug mode, the path is fixed to '/debug/collect'.
const path = (this.debugMode) ? '/debug/collect' : '/batch';
const requestOptions = {
method: 'POST',
responseType: 'json',
url: `${BASE_URL}${path}`,
body: payload,
headers: {'User-Agent': 'Tentacles/MeasurementProtocol-v1'}
};
const response = await request(requestOptions);
/** @type {BatchResult} */ const batchResult = {
numberOfLines: lines.length,
};
if (response.status < 200 || response.status >= 300) {
const errorMessages = [
`Measurement Protocol [${batchId}] didn't succeed.`,
`Get response code: ${response.status}`,
`response: ${response.data}`,
];
this.logger.error(errorMessages.join('\n'));
batchResult.errors = errorMessages;
batchResult.result = false;
return batchResult;
}
this.logger.debug(`Configuration:`, config);
this.logger.debug(`Input Data: `, lines);
this.logger.debug(`Batch[${batchId}] status: ${response.status}`);
this.logger.debug(response.data);
// There is not enough information from the non-debug mode.
if (!this.debugMode) {
batchResult.result = true;
} else {
this.extraFailedLines_(batchResult, response.data.hitParsingResult,
lines);
}
return batchResult;
};
};