Skip to content

Instantly share code, notes, and snippets.

@FlorianTopf
Created June 2, 2016 16:40
Show Gist options
  • Select an option

  • Save FlorianTopf/f6f35a1cc830758041f9f8ef8dece16f to your computer and use it in GitHub Desktop.

Select an option

Save FlorianTopf/f6f35a1cc830758041f9f8ef8dece16f to your computer and use it in GitHub Desktop.

Revisions

  1. FlorianTopf created this gist Jun 2, 2016.
    94 changes: 94 additions & 0 deletions analytics.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    ;(function () {
    'use strict';

    var app = this;
    var className = 'OrderProcessingAnalytics';

    // saving the start time
    var orderProcessingStartTime = 0;
    var orderProcessingStopTime = 0;
    var measuringInProgress = false;

    var currentSellerId = null;

    var currentOrderAmount = 0;
    var currentTargetStatus = null;
    var currentOriginStatus = 'pending';

    function getCurrentSellerFromDataLayer() {
    var sellerId = null;

    for (var item in window.dataLayer) {
    if (item.hasOwnProperty('sellerId')) {
    sellerId = item.sellerId;
    break;
    }
    }

    return sellerId === null ? null : parseInt(sellerId, 0);
    }

    function OrderProcessingAnalytics() {
    this.initialize()
    }

    app.services[className] = OrderProcessingAnalytics;

    OrderProcessingAnalytics.prototype = {
    initialize: function () {
    currentSellerId = getCurrentSellerFromDataLayer();
    },

    startMeasuringDelta: function () {
    var self = this;
    self.resetTemporaryData();
    orderProcessingStartTime = Date.now();
    measuringInProgress = true;
    },

    stopMeasuringDelta: function () {
    orderProcessingStopTime = Date.now();
    measuringInProgress = false;
    return orderProcessingStopTime - orderProcessingStartTime;
    },

    resetTemporaryData: function () {
    orderProcessingStartTime = 0;
    orderProcessingStopTime = 0;
    currentOrderAmount = 0;
    currentTargetStatus = null;
    },

    stopMeasuringDeltaAndPushData: function () {
    var self = this;

    if (false === measuringInProgress) {
    return;
    }

    var timeDifference = self.stopMeasuringDelta();

    if (window.hasOwnProperty('dataLayer')) {
    window.dataLayer.push({
    event: 'orderProcessing',
    startTime: moment(orderProcessingStartTime).format(),
    stopTime: moment(orderProcessingStopTime).format(),
    delta: timeDifference,
    sellerId: currentSellerId,
    originStatus: currentOriginStatus,
    targetStatus: currentTargetStatus,
    orderAmount: currentOrderAmount
    });
    }
    },

    setTargetStatus: function (targetStatus) {
    currentTargetStatus = targetStatus;
    },

    setOrderAmount: function (orderAmount) {
    currentOrderAmount = orderAmount;
    }
    }

    }).call(window.App);