# Google Tag Manager

**URL:** <https://community.f5.com/t/google-tag-manager/71210>\
**Category:** Technical Forums\
**Tags:** application-delivery\
**Created:** [December 14, 2022, 8:38pm UTC](https://community.f5.com/t/google-tag-manager/71210 "2022-12-14T20:38:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![avp4](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@avp4](https://community.f5.com/u/avp4)\
**Post date:** [December 14, 2022, 8:38pm UTC](https://community.f5.com/t/google-tag-manager/71210/1 "2022-12-14T20:38:16Z")

</div>

I’ve been asked to implement Google Tag Manager via an iRule. I have the Google code but am unsure how to implement. Does anyone have experience with this and can point me in the right direction? This seems a bit different than the standard Google Analytics found here -&nbsp;[Google Analytics script injection - DevCentral (f5.com)](https://community.f5.com/topic/290951)

---

<div class="post-metadata">

**Author:** ![JRahm](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/jrahm/32/3327_2.png) [@JRahm](https://community.f5.com/u/JRahm)\
**Post date:** [December 16, 2022, 3:23pm UTC](https://community.f5.com/t/google-tag-manager/71210/2 "2022-12-16T15:23:15Z")

</div>

Seems you would just need 2 substitutions instead of one, like:

```tcl
when HTTP_RESPONSE_DATA { 
    set search1 "<head>"
    set search2 "<body>"
    set tracking_id [class match -value -- $host equals HOST_TRACKING_MAPPING]
    if { $tracking_id eq "" } {
        set tracking_id $static::default_trackingid
    }
    HTTP::payload replace 0 $content_length [string map [list $search1 "<head>[subst -nocommands -nobackslashes [ifile get gtm_head.js]]" $search2 "<body>[subst -nocommands -nobackslashes [ifile get gtm_body.js]]"] [HTTP::payload]]
    HTTP::release
}

```

**Note** : _I’m just interpreting literally the notes here:&nbsp;[https://developers.google.com/tag-platform/tag-manager/web](https://developers.google.com/tag-platform/tag-manager/web). If there’s a reason it should not be at the top of head for performance or other reasons, find the slot you want it to sit and then make the line in head above that as your first search term._

Just working in the tlcsh with substitute values, this works out like this:

```tcl
% set http_payload "<html><head><title>Hi there</title></head><body><p>This is a sentence</p></body></html>"
<html><head><title>Hi there</title></head><body><p>This is a sentence</p></body></html>
% set search1 <head>
<head>
% set search2 <body>
<body>
% set insert1 "<script>GTM_head_script_here</script>"
<script>GTM_head_script_here</script>
% set insert2 "<script>GTM_body_script_here</script>"
<script>GTM_body_script_here</script>
% string map [list $search1 "<head>[subst -nocommands -nobackslashes $insert1]" $search2 "<body>[subst -nocommands -nobackslashes $insert2]"] $http_payload
<html><head><script>GTM_head_script_here</script><title>Hi there</title></head><body><script>GTM_body_script_here</script><p>This is a sentence</p></body></html>

```

---

<div class="post-metadata">

**Author:** ![Kai\_Wilke](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/kai_wilke/32/24646_2.png) [@Kai\_Wilke](https://community.f5.com/u/Kai_Wilke)\
**Post date:** [January 20, 2023, 8:41am UTC](https://community.f5.com/t/google-tag-manager/71210/3 "2023-01-20T08:41:36Z")

</div>

HI Jason,

[subst] is one of the most broken commands in TCL, try to stay away from it and use alternatives. Even with -nocommand option a [subst] will still perform command execution during varibale expansions.

For your examples I actually dont see any usecase to even use subst at all…

```tcl
set search1 <head>
set search2 <body>
set insert1 "<head><script>GTM_head_script_here</script>"
set insert2 "<body><script>GTM_body_script_here</script>"
set http_payload [string map [list $search1 $insert1 $search2 $insert2] $http_payload]

```

Cheers, Kai
