getInsertConversionFn()

in marketing-analytics/activation/common-libs/nodejs-common/src/apis/doubleclick_search.js [178:230]


  getInsertConversionFn(config) {
    /**
     * Sends a batch of hits to Search Ads 360.
     * @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 conversionTimestamp = new Date().getTime();
      const conversions = lines.map((line, index) => {
        const record = JSON.parse(line);
        return Object.assign(
            {
              // Default value, can be overwritten by the exported data.
              conversionTimestamp,
              // Default conversion Id should be unique in a single request.
              // See error code 0x0000011F at here:
              // https://developers.google.com/search-ads/v2/troubleshooting#conversion-upload-errors
              conversionId: conversionTimestamp + index,
            },
            config, record);
      });
      this.logger.debug('Configuration: ', config);
      /** @const {BatchResult} */
      const batchResult = {
        result: true,
        numberOfLines: lines.length,
      };
      try {
        const doubleclicksearch = await this.getApiClient();
        const response = await doubleclicksearch.conversion.insert(
            {requestBody: {conversion: conversions}}
        );
        this.logger.debug('Response: ', response);
        const insertedConversions = response.data.conversion.length;
        if (lines.length !== insertedConversions) {
          const errorMessage =
              `Conversions input/inserted: ${lines.length}/${insertedConversions}`;
          this.logger.warn(errorMessage);
          batchResult.result = false;
          batchResult.numberOfLines = insertedConversions;
          batchResult.errors = [errorMessage];
        }
        this.logger.info(
            `SA[${batchId}] Insert ${insertedConversions} conversions.`);
        return batchResult;
      } catch (error) {
        this.updateBatchResultWithError_(batchResult, error, lines);
        return batchResult;
      }
    };
  }