Skip to content

Commit

Permalink
se#761 se#791 se#780 Create ga loader and move it to RF side (#314)
Browse files Browse the repository at this point in the history
* Create ga loader

* Create reverse_custom_page

* Return missed reverse function
  • Loading branch information
ArtemijRodionov authored Mar 25, 2019
1 parent dd11320 commit 19d6343
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
37 changes: 37 additions & 0 deletions ecommerce/front/trackers.es6
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
}
6 changes: 6 additions & 0 deletions pages/urls.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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/
Expand Down

0 comments on commit 19d6343

Please sign in to comment.