Skip to content

Commit

Permalink
Set site ID and tracker URL before including piwik.js
Browse files Browse the repository at this point in the history
  • Loading branch information
henkelund committed Jan 24, 2017
1 parent 1dda196 commit d94793a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
7 changes: 5 additions & 2 deletions view/frontend/templates/piwik.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@
// event which breaks the link tracking feature.
?>
<script type="text/javascript">
(function (d) {
(function (w, d) {
w._paq = w._paq || [];
w._paq.push(['setTrackerUrl', '<?php echo $block->escapeJsQuote($block->getTrackerUrl()); ?>']);
w._paq.push(['setSiteId', <?php echo (int) $block->getSiteId(); ?>]);
var g = d.createElement('script'),
s = d.getElementsByTagName('script')[0];
g.type = 'text/javascript';
g.async = true;
g.defer = true;
g.src = '<?php echo $block->escapeJsQuote($block->getScriptUrl()); ?>';
s.parentNode.insertBefore(g, s);
})(document);
})(window, document);
</script>
<noscript>
<p>
Expand Down
37 changes: 18 additions & 19 deletions view/frontend/web/js/tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,14 @@ define([
* Append Piwik tracker script URL to head
*
* @param {String} scriptUrl
* @return bool Wheter the script was injected
*/
function injectScript(scriptUrl) {
if (piwik === null) {
$('<script>')
.attr('type', 'text/javascript')
.attr('async', true)
.attr('defer', true)
.attr('src', scriptUrl)
.appendTo('head');
return true;
}
return false; // Script is already included
$('<script>')
.attr('type', 'text/javascript')
.attr('async', true)
.attr('defer', true)
.attr('src', scriptUrl)
.appendTo('head');
}

/**
Expand Down Expand Up @@ -236,16 +231,20 @@ define([
* @param {Object} options
*/
function initialize(options) {
pushAction([
['setSiteId', defaultSiteId = options.siteId],
['setTrackerUrl', defaultTrackerUrl = options.trackerUrl]
]);
pushAction(options.actions);
if (!injectScript(options.scriptUrl)) {
// If the tracker script was not injected we already have the Piwik
// object and can resolve promises immediately.
defaultSiteId = options.siteId;
defaultTrackerUrl = options.trackerUrl;
if (piwik === null) {
pushAction([
['setSiteId', defaultSiteId],
['setTrackerUrl', defaultTrackerUrl]
]);
injectScript(options.scriptUrl);
} else {
// If we already have the Piwik object we can resolve any pending
// promises immediately.
resolvePiwikPromises();
}
pushAction(options.actions);
}

// Make sure the Piwik asynchronous tracker queue is defined
Expand Down

0 comments on commit d94793a

Please sign in to comment.