Skip to content
This repository was archived by the owner on Jun 25, 2024. It is now read-only.

Commit 92d9bbe

Browse files
author
stevebagwell
committed
make distinct empty functions for the google analytics scripts
1 parent 5e82eac commit 92d9bbe

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

themes/material/common-head-elements.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
$hasGATracking = false;
1111

1212
if (! empty($trackingId)) {
13-
$hasGATracking = true;
1413
?>
1514
<script>
1615
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
@@ -19,16 +18,22 @@
1918
</script>
2019
<script async src='https://www.google-analytics.com/analytics.js'></script>
2120
<?php
21+
} else {
22+
?>
23+
<script>
24+
window.ga = function () {
25+
// Null object pattern to avoid `if (window.ga)` wherever ga is used.
26+
}
27+
</script>
28+
<?php
2229
}
2330
?>
2431

25-
2632
<?php
2733
// This block of code is intended to be temporary until the transition from
2834
// Google's Universal Analytics to GA4 type projects has been completed
2935
$trackingIdGA4 = htmlentities($this->configuration->getValue('analytics.trackingIdGA4'));
3036
if (! empty($trackingIdGA4)) {
31-
$hasGATracking = true;
3237
?>
3338
<!-- Google tag (gtag.js) -->
3439
<script async src="https://www.googletagmanager.com/gtag/js?id=<?= $trackingIdGA4 ?>"></script>
@@ -40,13 +45,11 @@ function gtag(){dataLayer.push(arguments);}
4045
gtag('config', '<?= $trackingIdGA4 ?>');
4146
</script>
4247
<?php
43-
}
44-
45-
if (! $hasGATracking) {
48+
} else {
4649
?>
4750
<script>
48-
window.ga = function () {
49-
// Null object pattern to avoid `if (window.ga)` wherever ga is used.
51+
window.dataLayer = function () {
52+
// Null object pattern to avoid `if (window.dataLayer)` wherever dataLayer is used.
5053
}
5154
</script>
5255
<?php

0 commit comments

Comments
 (0)