Insert Google-Tag in SMF Simplemachines Forum

Recently I had set up a new SMF forum (Simplemachines) and wanted to insert the Google tag for Google Analytics.
So I just copied the Google tag and pasted it in the index.template.php file.But unfortunately that didn’t work and all I saw afterwards on the forum was a white page.It took me a while to find the solution.

This is the code you get from Google and it doesn’t work:

<!– Google tag (gtag.js) –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX”></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);} gtag(‘js’, new Date());
gtag(‘config’, ‘G-XXXXXXXXXX’);
</script>

 

For the code to work you have to put a backslash (\) in front of each single quotation mark (‘).
The correct script looks like this:

 

<!– Google tag (gtag.js) –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX”></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(\’js\’, new Date());

gtag(\’config\’, \’G-XXXXXXXXXX\’);
</script>

This worked in my case. Let us know if you had success with this solution.

Leave a Reply

Your email address will not be published. Required fields are marked *