summaryrefslogtreecommitdiff
path: root/addons/website_sale/static/src/js/website_sale_tracking.js
blob: 92850707755478d6c5b9f866a8858a582fdcebc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
odoo.define('website_sale.tracking', function (require) {

var publicWidget = require('web.public.widget');

publicWidget.registry.websiteSaleTracking = publicWidget.Widget.extend({
    selector: '.oe_website_sale',
    events: {
        'click form[action="/shop/cart/update"] a.a-submit': '_onAddProductIntoCart',
        'click a[href="/shop/checkout"]': '_onCheckoutStart',
        'click div.oe_cart a[href^="/web?redirect"][href$="/shop/checkout"]': '_onCustomerSignin',
        'click form[action="/shop/confirm_order"] a.a-submit': '_onOrder',
        'click form[target="_self"] button[type=submit]': '_onOrderPayment',
    },

    /**
     * @override
     */
    start: function () {
        var self = this;

        // Watching a product
        if (this.$el.is('#product_detail')) {
            var productID = this.$('input[name="product_id"]').attr('value');
            this._vpv('/stats/ecom/product_view/' + productID);
        }

        // ...
        if (this.$('div.oe_website_sale_tx_status').length) {
            this._trackGA('require', 'ecommerce');

            var orderID = this.$('div.oe_website_sale_tx_status').data('order-id');
            this._vpv('/stats/ecom/order_confirmed/' + orderID);

            this._rpc({
                route: '/shop/tracking_last_order/',
            }).then(function (o) {
                self._trackGA('ecommerce:clear');

                if (o.transaction && o.lines) {
                    self._trackGA('ecommerce:addTransaction', o.transaction);
                    _.forEach(o.lines, function (line) {
                        self._trackGA('ecommerce:addItem', line);
                    });
                }
                self._trackGA('ecommerce:send');
            });
        }

        return this._super.apply(this, arguments);
    },

    //--------------------------------------------------------------------------
    // Private
    //--------------------------------------------------------------------------

    /**
     * @private
     */
    _trackGA: function () {
        var websiteGA = window.ga || function () {};
        websiteGA.apply(this, arguments);
    },
    /**
     * @private
     */
    _vpv: function (page) { //virtual page view
        this._trackGA('send', 'pageview', {
          'page': page,
          'title': document.title,
        });
    },

    //--------------------------------------------------------------------------
    // Handlers
    //--------------------------------------------------------------------------

    /**
     * @private
     */
    _onAddProductIntoCart: function () {
        var productID = this.$('input[name="product_id"]').attr('value');
        this._vpv('/stats/ecom/product_add_to_cart/' + productID);
    },
    /**
     * @private
     */
    _onCheckoutStart: function () {
        this._vpv('/stats/ecom/customer_checkout');
    },
    /**
     * @private
     */
    _onCustomerSignin: function () {
        this._vpv('/stats/ecom/customer_signin');
    },
    /**
     * @private
     */
    _onOrder: function () {
        if ($('#top_menu [href="/web/login"]').length) {
            this._vpv('/stats/ecom/customer_signup');
        }
        this._vpv('/stats/ecom/order_checkout');
    },
    /**
     * @private
     */
    _onOrderPayment: function () {
        var method = $('#payment_method input[name=acquirer]:checked').nextAll('span:first').text();
        this._vpv('/stats/ecom/order_payment/' + method);
    },
});
});