Skip to content

Commit

Permalink
Added support for Google gtag.js via html.gtag
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogdani committed Sep 28, 2021
1 parent 8016367 commit 1c6628a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/tmpl_html.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,22 @@ E.g.: `{{ html.getVersion }}`
```
---

#### gtag()
Returns javascript code for Google Global Tag (gtag.js). Google Analytics id must be provided in the site configuration file. A domain is provided in the configuration file; the code will be shown only if current domain matches the provided domain. This is very useful in test installations.

E.g.: `{{ html.gtag }}`

```javascript
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA-ID"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'GA-ID');
</script>
```
---
#### GUA()
Returns javascript code for Google Universal Analytics Tracking. Google Analytics id must be provided in the site configuration file. A domain is provided in the configuration file; the code will be shown only if current domain matches the provided domain. This is very useful in test installations.

Expand Down
26 changes: 26 additions & 0 deletions lib/OutHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,32 @@ public function GUA()
return $html;
}

/**
* Returns html containing Google Global Tag (gtag.js)
* @return string
*/
public function gtag()
{
$id = cfg::get('ga_id');

$restrict_domain = cfg::get('ga_domain');


if ($id && (!$restrict_domain || (preg_match('/' . $restrict_domain . '/', $_SERVER['HTTP_HOST']))) && !$this->cfg['isDraft']) {
$html = '<!-- Global site tag (gtag.js) - Google Analytics -->' .
'<script async src="https://www.googletagmanager.com/gtag/js?id=' . $id . '"></script>' .
'<script>' .
'window.dataLayer = window.dataLayer || [];' .
'function gtag(){dataLayer.push(arguments);}' .
"gtag('js', new Date());" .

"gtag('config', '" . $id . "');".
'</script>';
}

return $html;
}


/**
* Returns html with image gallery. Alias for customTags:gallery()
Expand Down
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
5.8.0[] = "Added support for Google gtag.js via html.gtag"
5.7.1[] = "Removed debugging error_log in template_ctrl"
5.7.0[] = "Changed format of logAttempts, and rised time buffer to 2 seconds"
5.6.2[] = "Optimized calculation of path of assets"
Expand Down

0 comments on commit 1c6628a

Please sign in to comment.