diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/website_sale/static/src/js/website_sale_tracking.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website_sale/static/src/js/website_sale_tracking.js')
| -rw-r--r-- | addons/website_sale/static/src/js/website_sale_tracking.js | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/addons/website_sale/static/src/js/website_sale_tracking.js b/addons/website_sale/static/src/js/website_sale_tracking.js new file mode 100644 index 00000000..92850707 --- /dev/null +++ b/addons/website_sale/static/src/js/website_sale_tracking.js @@ -0,0 +1,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); + }, +}); +}); |
