<commit_before>import cleanObject from 'driveback-utils/cleanObject';
import { getProp } from 'driveback-utils/dotProp';
import {
  getEnrichableVariableMappingProps,
  extractVariableMappingValues,
} from '../IntegrationUtils';

import Integration from '../Integration';
import {
  VIEWED_PAGE,
  VIEWED_PRODUCT_DETAIL,
  VIEWED_PRODUCT_LISTING,
  ADDED_PRODUCT,
  REMOVED_PRODUCT,
  COMPLETED_TRANSACTION,
  SEARCHED_PRODUCTS,
} from '../events/semanticEvents';

const SEMANTIC_EVENTS = [
  VIEWED_PAGE,
  VIEWED_PRODUCT_DETAIL,
  VIEWED_PRODUCT_LISTING,
  ADDED_PRODUCT,
  REMOVED_PRODUCT,
  COMPLETED_TRANSACTION,
  SEARCHED_PRODUCTS,
];

class REES46 extends Integration {
  constructor(digitalData, options) {
    const optionsWithDefaults = Object.assign({
      storeKey: '',
      feedWithGroupedProducts: false,
      productVariables: {},
    }, options);

    super(digitalData, optionsWithDefaults);

    this.addTag({
      type: 'script',
      attr: {
        src: 'https://cdn.rees46.com/v3.js',
      },
    });
  }

  initialize() {
    window.r46 = window.r46 || function r46Init() {
      (window.r46.q = window.r46.q || []).push(arguments);
    };
    window.r46('init', this.getOption('storeKey'));
  }

  getSemanticEvents() {
    return SEMANTIC_EVENTS;
  }

  getEnrichableEventProps(event) {
    switch (event.name) {
      case VIEWED_PAGE: return ['user', 'website'];
      case VIEWED_PRODUCT_LISTING: return ['listing.categoryId'];
      case SEARCHED_PRODUCTS: return ['listing.query'];
      case COMPLETED_TRANSACTION: return ['transaction'];
      case VIEWED_PRODUCT_DETAIL:
        return [
          ...getEnrichableVariableMappingProps(this.getOption('productVariables')),
          'product',
        ];
      default: return [];
    }
  }

  getProductVariables(event) {
    const { product } = event;
    const mapping = this.getOption('productVariables');
    return extractVariableMappingValues(
      { event, product },
      mapping,
      { multipleScopes: true },
    );
  }

  trackEvent(event) {
    const eventMap = {
      [VIEWED_PAGE]: this.onViewedPage.bind(this),
      [VIEWED_PRODUCT_DETAIL]: this.onViewedProductDetail.bind(this),
      [VIEWED_PRODUCT_LISTING]: this.onViewedProductListing.bind(this),
      [SEARCHED_PRODUCTS]: this.onSearchedProducts.bind(this),
      [ADDED_PRODUCT]: this.onAddedProduct.bind(this),
      [REMOVED_PRODUCT]: this.onRemovedProduct.bind(this),
      [COMPLETED_TRANSACTION]: this.onCompletedTransaction.bind(this),
    };
    if (eventMap[event.name]) {
      eventMap[event.name](event);
    }
  }

  onViewedPage(event) {
    const { user } = event;
    if (!user || !user.userId || !user.email) return;
    const gender = String(user.gender).toLowerCase();

    const rees46Data = cleanObject({
      id: user.userId,
      email: user.email,
      gender: ['m', 'f'].indexOf(gender) >= 0 ? gender : undefined,
      birthday: user.birthDate,
      location: getProp(event, 'website.regionId'),
    });

    window.r46('profile', 'set', rees46Data);
  }

  onViewedProductDetail(event) {
    const product = event.product || {};
    const feedWithGroupedProducts = this.getOption('feedWithGroupedProducts');
    const productId = (feedWithGroupedProducts) ? product.skuCode : product.id;
    if (!productId) return;

    const productVariables = this.getProductVariables(event);
    const { stock } = product;
    if (stock !== undefined) {
      productVariables.stock = (stock > 0);
    }

    if (Object.keys(productVariables).length > 0) {
      window.r46('track', 'view', {
        id: productId,
        ...productVariables,
      });
    } else {
      window.r46('track', 'view', productId);
    }
  }
<commit_msg>DRI-796 fix rees46 Viewed Page event for anonimous user<commit_after>import cleanObject from 'driveback-utils/cleanObject';
import { getProp } from 'driveback-utils/dotProp';
import {
  getEnrichableVariableMappingProps,
  extractVariableMappingValues,
} from '../IntegrationUtils';

import Integration from '../Integration';
import {
  VIEWED_PAGE,
  VIEWED_PRODUCT_DETAIL,
  VIEWED_PRODUCT_LISTING,
  ADDED_PRODUCT,
  REMOVED_PRODUCT,
  COMPLETED_TRANSACTION,
  SEARCHED_PRODUCTS,
} from '../events/semanticEvents';

const SEMANTIC_EVENTS = [
  VIEWED_PAGE,
  VIEWED_PRODUCT_DETAIL,
  VIEWED_PRODUCT_LISTING,
  ADDED_PRODUCT,
  REMOVED_PRODUCT,
  COMPLETED_TRANSACTION,
  SEARCHED_PRODUCTS,
];

class REES46 extends Integration {
  constructor(digitalData, options) {
    const optionsWithDefaults = Object.assign({
      storeKey: '',
      feedWithGroupedProducts: false,
      productVariables: {},
    }, options);

    super(digitalData, optionsWithDefaults);

    this.addTag({
      type: 'script',
      attr: {
        src: 'https://cdn.rees46.com/v3.js',
      },
    });
  }

  initialize() {
    window.r46 = window.r46 || function r46Init() {
      (window.r46.q = window.r46.q || []).push(arguments);
    };
    window.r46('init', this.getOption('storeKey'));
  }

  getSemanticEvents() {
    return SEMANTIC_EVENTS;
  }

  getEnrichableEventProps(event) {
    switch (event.name) {
      case VIEWED_PAGE: return ['user', 'website'];
      case VIEWED_PRODUCT_LISTING: return ['listing.categoryId'];
      case SEARCHED_PRODUCTS: return ['listing.query'];
      case COMPLETED_TRANSACTION: return ['transaction'];
      case VIEWED_PRODUCT_DETAIL:
        return [
          ...getEnrichableVariableMappingProps(this.getOption('productVariables')),
          'product',
        ];
      default: return [];
    }
  }

  getProductVariables(event) {
    const { product } = event;
    const mapping = this.getOption('productVariables');
    return extractVariableMappingValues(
      { event, product },
      mapping,
      { multipleScopes: true },
    );
  }

  trackEvent(event) {
    const eventMap = {
      [VIEWED_PAGE]: this.onViewedPage.bind(this),
      [VIEWED_PRODUCT_DETAIL]: this.onViewedProductDetail.bind(this),
      [VIEWED_PRODUCT_LISTING]: this.onViewedProductListing.bind(this),
      [SEARCHED_PRODUCTS]: this.onSearchedProducts.bind(this),
      [ADDED_PRODUCT]: this.onAddedProduct.bind(this),
      [REMOVED_PRODUCT]: this.onRemovedProduct.bind(this),
      [COMPLETED_TRANSACTION]: this.onCompletedTransaction.bind(this),
    };
    if (eventMap[event.name]) {
      eventMap[event.name](event);
    }
  }

  onViewedPage(event) {
    const user = event.user || {};
    const location = getProp(event, 'website.regionId');
    const gender = String(user.gender).toLowerCase();

    const rees46Data = cleanObject({
      id: user.userId,
      email: user.email,
      gender: ['m', 'f'].indexOf(gender) >= 0 ? gender : undefined,
      birthday: user.birthDate,
      location,
    });

    if (Object.keys(rees46Data).length > 0) {
      window.r46('profile', 'set', rees46Data);
    }
  }

  onViewedProductDetail(event) {
    const product = event.product || {};
    const feedWithGroupedProducts = this.getOption('feedWithGroupedProducts');
    const productId = (feedWithGroupedProducts) ? product.skuCode : product.id;
    if (!productId) return;

    const productVariables = this.getProductVariables(event);
    const { stock } = product;
    if (stock !== undefined) {
      productVariables.stock = (stock > 0);
    }

    if (Object.keys(productVariables).length > 0) {
      window.r46('track', 'view', {
        id: productId,
        ...productVariables,
      });
    } else {
      window.r46('track', 'view', productId);
    }
  }

{'fraction_non_alphanumeric': 0.1304232100079851, 'fraction_numerical': 0.00904977375565611, 'mean_word_length': 3.3314121037463975, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1751401', 'n_tokens_mistral': 2525, 'n_tokens_neox': 2412, 'n_words': 416}