Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* global sgAnalytics */
- import SgTrackingPlugin from '@shopgate/tracking-core/plugins/Base';
- import configPR from '../../config.json';
- import { getUserData } from '@shopgate/pwa-common/selectors/user';
- /**
- * Tracking plugin to handle internal event tracking to the shopgate analytics sdk.
- */
- class CheckoutBeacon extends SgTrackingPlugin {
- /**
- * Initializes the tracking plugin.
- * @param {Object} options The options passed to the plugin.
- * @param {string} options.stage The stage / environment the plugin is running.
- * @param {string} options.shopNumber The shop number.
- * @param {string} [options.userId] The internal user id.
- * @param {string} [options.pushToken] The push token of the device.
- * @param {string} [options.access] The access type (App, Web).
- */
- constructor({
- stage,
- shopNumber,
- userId,
- pushToken,
- access,
- }) {
- super('CheckoutBeacon', { stage });
- const userEmail = getUserData(state).mail || null;
- const userFirstName = getUserData(state).first_name || null;
- const userLastName = getUserData(state).last_name || null;
- this.registerEvents();
- }
- /**
- * Registers all event handlers.
- *
- * Event documentation:
- * https://wiki.shopgate.guru/display/TEAMC2/Tracking+Service+Events
- */
- registerEvents() {
- this.register.purchase((data, rawData) => {
- const script = document.createElement('script');
- script.src = `//static.powerreviews.com/t/v1/tracker.js`;
- script.async = false;
- document.querySelector('head').appendChild(script);
- script.onload = () => {
- (function(){try{
- var tracker = POWERREVIEWS.tracker.createTracker({
- 'merchantGroupId': configPR.configuration.apiKey.params.options.merchantGroupId});
- var orderFeed = {
- 'merchantGroupId': configPR.configuration.apiKey.params.options.merchantGroupId,
- 'merchantId': configPR.configuration.apiKey.params.options.merchantId,
- 'locale': configPR.configuration.apiKey.params.options.locale,
- 'marketingOptIn': true,
- 'userEmail': this.userEmail,
- 'userFirstName': this.userFirstName,
- 'userLastName': this.userLastName,
- 'orderId': rawData.order.number,
- 'orderItems': rawData.order.products.map(product => ({
- 'page_id': product.uid || product.productNumber,
- 'quantity': product.quantity,
- 'unit_price': product.amount.displayPrice ||
- product.amount.grosss ||
- product.amount.net,
- })),
- }
- tracker.trackCheckout(orderFeed);
- }catch(e){window.console && window.console.log(e)}}());
- };
- });
- }
- }
- export default CheckoutBeacon;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement