From 19d6343f0d381be771b83a28990a3afc0119bd19 Mon Sep 17 00:00:00 2001 From: Artemiy Date: Tue, 26 Mar 2019 01:22:16 +0300 Subject: [PATCH] se#761 se#791 se#780 Create ga loader and move it to RF side (#314) * Create ga loader * Create reverse_custom_page * Return missed reverse function --- ecommerce/front/trackers.es6 | 37 ++++++++++++++++++++++++++++++++++++ pages/urls.py | 6 ++++++ 2 files changed, 43 insertions(+) diff --git a/ecommerce/front/trackers.es6 b/ecommerce/front/trackers.es6 index b142cdd..ceb9016 100644 --- a/ecommerce/front/trackers.es6 +++ b/ecommerce/front/trackers.es6 @@ -54,3 +54,40 @@ class GATracker extends Tracker { this.transport(`${this.name}:${actionName}`, data); } } + + +/** + * Reliable loader of Google Analytics scripts. + * + * Google Analytics scripts (GA) are loading by the Google tag manager (GTM). + * If GA is still not loaded, the function will delay execution until it is loaded. + */ +function loadGaTransport(onLoadName) { + var state = { + loaded: false, + delayed: [], + }; + + let load = () => { + ga('require', 'ecommerce'); + state.loaded = true; + }; + + try { + load(); + } catch (e) { + window.addEventListener(onLoadName, () => { + load(); + // submit delayed transactions + state.delayed.forEach((args) => ga(...args)); + }); + } + + return (...args) => { + if (state.loaded) { + ga(...args); + } else { + state.delayed.push(args); + } + }; +} diff --git a/pages/urls.py b/pages/urls.py index ac4f3b1..5ac2834 100644 --- a/pages/urls.py +++ b/pages/urls.py @@ -1,6 +1,7 @@ from functools import partial from django.conf.urls import url +from django.urls import reverse from pages import views from pages.models import CustomPage @@ -9,6 +10,11 @@ custom_page_url = partial(url, name=CustomPage.ROUTE) + +def reverse_custom_page(name): + return reverse(CustomPage.ROUTE, kwargs={'page': name}) + + urlpatterns = [ url(r'^$', views.FlatPageView.as_view(), name='index'), # /one/