getUploadConversionFn()

in marketing-analytics/activation/common-libs/nodejs-common/src/apis/dfa_reporting.js [152:246]


  getUploadConversionFn(config) {
    /**
     * Sends a batch of hits to Campaign Manager.
     * @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) => {
      /** @type {function} Gets the conversion elements from the data object. */
      const filterObject = getFilterFunction(PICKED_PROPERTIES);
      const time = new Date().getTime();
      const operation =
        config.operation === 'update' ? 'batchupdate' : 'batchinsert';
      // customVariables is not supported by batchupdate.
      // https://developers.google.com/doubleclick-advertisers/rest/v4/Conversion#CustomFloodlightVariable
      if (operation === 'batchupdate' &&
        typeof config.customVariables !== 'undefined') {
        this.logger.warn('customVariables is not supported by batchupdate');
        delete config.customVariables;
      }
      const conversions = lines.map((line) => {
        const record = JSON.parse(line);
        const conversion = Object.assign(
            {
              // Default value, can be overwritten by the exported data.
              ordinal: time,
              timestampMicros: time * 1000,
            },
            config.conversion, filterObject(record));
        conversion[config.idType] = record[config.idType];
        // Custom Variables
        if (typeof config.customVariables !== 'undefined') {
          conversion.customVariables = config.customVariables.map(
            (variable) => ({ 'type': variable, 'value': record[variable], }));
        }
        // User Identifiers
        if (record.userIdentifiers) {
          const userIdentifiers = [];
          IDENTIFIERS.map((id) => {
            const idValue = record.userIdentifiers[id];
            if (idValue) {
              if (Array.isArray(idValue)) {
                idValue.forEach(
                  (user) => void userIdentifiers.push({ [id]: user }));
              } else {
                userIdentifiers.push({ [id]: idValue });
              }
            }
          });
          if (userIdentifiers.length > 0) {
            if (userIdentifiers.length > MAX_IDENTIFIERS_PER_USER) {
              this.logger.warn(
                'There are too many user identifiers:', record.userIdentifiers);
            }
            conversion.userIdentifiers =
              userIdentifiers.slice(0, MAX_IDENTIFIERS_PER_USER);
          } else {
            this.logger.warn(
              'There is no valid user identifier:', record.userIdentifiers);
          }
        }
        return conversion;
      });
      const requestBody = {conversions};
      if (config.idType === 'encryptedUserId') {
        requestBody.encryptionInfo = config.encryptionInfo;
      }
      /** @const {BatchResult} */
      const batchResult = {
        result: true,
        numberOfLines: lines.length,
      };
      try {
        const dfareporting = await this.getApiClient();
        const response = await dfareporting.conversions[operation]({
          profileId: config.profileId,
          requestBody: requestBody,
        });
        const failed = response.data.hasFailures;
        if (failed) {
          this.logger.warn(`CM [${batchId}] has failures.`);
          this.extraFailedLines_(batchResult, response.data.status, lines);
        }
        this.logger.debug('Configuration: ', config);
        this.logger.debug('Response: ', response);
        return batchResult;
      } catch (error) {
        this.logger.error(`CM[${batchId}] failed.`, error);
        batchResult.result = false;
        batchResult.errors = [error.message || error.toString()];
        return batchResult;
      }
    };
  };