Advertisement
kotvalera83

Untitled

Nov 7th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* global sgAnalytics */
  2. import SgTrackingPlugin from '@shopgate/tracking-core/plugins/Base';
  3. import configPR from  '../../config.json';
  4. import { getUserData } from '@shopgate/pwa-common/selectors/user';
  5. /**
  6.  * Tracking plugin to handle internal event tracking to the shopgate analytics sdk.
  7.  */
  8. class CheckoutBeacon extends SgTrackingPlugin {
  9.   /**
  10.    * Initializes the tracking plugin.
  11.    * @param {Object} options The options passed to the plugin.
  12.    * @param {string} options.stage The stage / environment the plugin is running.
  13.    * @param {string} options.shopNumber The shop number.
  14.    * @param {string} [options.userId] The internal user id.
  15.    * @param {string} [options.pushToken] The push token of the device.
  16.    * @param {string} [options.access] The access type (App, Web).
  17.    */
  18.   constructor({
  19.     stage,
  20.     shopNumber,
  21.     userId,
  22.     pushToken,
  23.     access,
  24.   }) {
  25.     super('CheckoutBeacon', { stage });
  26.     const userEmail = getUserData(state).mail || null;
  27.     const userFirstName = getUserData(state).first_name || null;
  28.     const userLastName = getUserData(state).last_name || null;
  29.     this.registerEvents();
  30.   }
  31.  
  32.   /**
  33.    * Registers all event handlers.
  34.    *
  35.    * Event documentation:
  36.    * https://wiki.shopgate.guru/display/TEAMC2/Tracking+Service+Events
  37.    */
  38.   registerEvents() {
  39.  
  40.     this.register.purchase((data, rawData) => {
  41.       const script = document.createElement('script');
  42.       script.src = `//static.powerreviews.com/t/v1/tracker.js`;
  43.       script.async = false;
  44.       document.querySelector('head').appendChild(script);
  45.       script.onload = () => {
  46.           (function(){try{
  47.               var tracker = POWERREVIEWS.tracker.createTracker({
  48.                   'merchantGroupId': configPR.configuration.apiKey.params.options.merchantGroupId});
  49.                   var orderFeed = {
  50.                       'merchantGroupId': configPR.configuration.apiKey.params.options.merchantGroupId,
  51.                       'merchantId': configPR.configuration.apiKey.params.options.merchantId,
  52.                       'locale': configPR.configuration.apiKey.params.options.locale,
  53.                       'marketingOptIn': true,
  54.                       'userEmail': this.userEmail,
  55.                       'userFirstName': this.userFirstName,
  56.                       'userLastName': this.userLastName,
  57.                       'orderId': rawData.order.number,
  58.                       'orderItems': rawData.order.products.map(product => ({
  59.                         'page_id': product.uid || product.productNumber,
  60.                         'quantity': product.quantity,
  61.                         'unit_price': product.amount.displayPrice ||
  62.                         product.amount.grosss ||
  63.                         product.amount.net,
  64.                       })),
  65.                   }
  66.          
  67.                   tracker.trackCheckout(orderFeed);
  68.                  
  69.           }catch(e){window.console && window.console.log(e)}}());
  70.       };
  71.     });
  72.   }
  73. }
  74.  
  75. export default CheckoutBeacon;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement