Embed Google Tag Manager by extending TYPO3 Site Module

Georg Ringer
Jan 18 · 2 min read

This blog post describes how the Sites module of TYPO3 9 can be extended and how the information can be extracted and used later on.

Additional tab with one extra field to define the Google Tag Manager code

TYPO3 9 LTS ships a new module called Sites.
This is the central place to define all relevant information regarding domains and available languages.

Setup

Extensions can extend the site configuration with additional fields. To be able to provide custom configuration, the following file needs to placed in your site-package extension at Configuration/SiteConfiguration/Overrides/sites.php.


You will recognize the syntax from writing TCA code as it is very similar.
However, there are certain limits regarding the available field types which are written down in the docs. In short, you can use input & select fields and checkboxes but for example no relations.

Usage

The value of the new field can be retrieved in various ways.

PHP

The SiteFinder can be used in Frontend or Backend to retrieve a proper Site object — either by providing a page id, root page id or the site identifier.
The Site itself includes all information which is persisted in the Sites module.

TypoScript

The shortest possible snippet can look like this:

lib.test = TEXT
lib.test.data = site:googleTagManager

A real-world example would be the following which retrieves the value and uses it for including the Google Tag Manager into the TYPO3 site.

Happy coding!

Georg Ringer

Written by

Software developer, primary working with the CMS TYPO3, freelancer and employed at https://www.studiomitte.com in Linz (Austria)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade