webman
webman

Reputation: 1203

TYPO3 set Google Analytics tag based on domain with typoscript

I have a multidomain where I'd like to set the individual Google Analytics tag with typoscript setup (TYPO3 V11).

I start with constants.typoscript:

myExt {
  googleAnalytics {
    default = G-SITE1
    site2 = G-SITE2
  }
}

followed by setup.typoscript which works fine:

# google analytics
page.headerData.3 = TEXT
page.headerData.3.value(
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id={$myExt.googleAnalytics.default}"></script>
    ...
)

I thought the following should work, but all variants I tried failed:

# Multidomain
config.googleAnalytics = {$myExt.googleAnalytics.default}

[request.getNormalizedParams().getHttpHost() == "site2.com"]
    config.googleAnalytics = {$myExt.googleAnalytics.site2}
[END]

# google analytics
page.headerData.3 = TEXT
page.headerData.3.value(
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id={$config.googleAnalytics}"></script>
    ...
)

Upvotes: 0

Views: 61

Answers (1)

Heinz Schilling
Heinz Schilling

Reputation: 2262

You can set constants values based on root page uid of the different domains.

constants.typoscript

myExt.googleAnalytics = G-SITE1

[tree.rootLine[0]["uid"] == ROOT-PAGE-UID]
    myExt.googleAnalytics = G-SITE2
[END]

setup.typoscript

# google analytics
page.headerData.3 = TEXT
page.headerData.3.value(
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id={$myExt.googleAnalytics}"></script>
    ...
)

Docs about condition: https://docs.typo3.org/m/typo3/reference-typoscript/11.5/en-us/Conditions/Index.html#tree-rootline

Upvotes: 0

Related Questions