Skip to content

Commit 4a38162

Browse files
authored
Merge pull request #36 from MHundeloh/master
Do not insert an additional trackPageView if already done by twig function
2 parents b9a672f + 8267622 commit 4a38162

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Twig/Extension.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,17 @@ public function piwikCode()
107107
private function addDefaultApiCalls()
108108
{
109109
$this->paqs[] = ['enableLinkTracking'];
110-
111-
foreach ($this->paqs as $paq) {
110+
$manuallyAddedTrackPageView = null;
111+
foreach ($this->paqs as $i => $paq) {
112+
if ('trackPageView' === $paq[0]) {
113+
/*
114+
* It is recommended to "trackPageView" e.g. after setDocumentTitle.
115+
* See https://matomo.org/faq/how-to/how-do-i-set-a-custom-page-title-using-the-matomo-javascript-title/
116+
* So, let's rearrange the paqs
117+
*/
118+
$manuallyAddedTrackPageView = $paq;
119+
unset($this->paqs[$i]);
120+
}
112121
if ('trackSiteSearch' === $paq[0]) {
113122
/*
114123
* It is recommended *not* to "trackPageView" for "trackSiteSearch" pages.
@@ -118,7 +127,10 @@ private function addDefaultApiCalls()
118127
return; // Do not add 'trackPageView'
119128
}
120129
}
121-
122-
$this->paqs[] = ['trackPageView'];
130+
if (null !== $manuallyAddedTrackPageView) {
131+
$this->paqs[] = $manuallyAddedTrackPageView; // move manually added 'trackPageView' to the end
132+
} else {
133+
$this->paqs[] = ['trackPageView'];
134+
}
123135
}
124136
}

0 commit comments

Comments
 (0)