Skip to content

Commit

Permalink
fix(docker): fix escaping for Google Tag Manager (#5131)
Browse files Browse the repository at this point in the history
  • Loading branch information
char0n authored Jun 11, 2024
1 parent ee2629d commit f07668a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions docker-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ fi
if [[ "${GTM}" ]]; then
GTM_SCRIPT="<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','${GTM}');</script>"
GTM_NOSCRIPT="<noscript><iframe src=\"https://www.googletagmanager.com/ns.html?id=${GTM}\" height=\"0\" width=\"0\" style=\"display:none;visibility:hidden\"></iframe></noscript>"
sed -i "s~<!-- Google Tag Manager -->~$GTM_SCRIPT~" $INDEX_FILE
sed -i "s~<!-- Google Tag Manager (noscript) -->~$GTM_NOSCRIPT~" $INDEX_FILE

# Escape the strings for sed inline
GTM_SCRIPT_ESCAPED=$(echo "$GTM_SCRIPT" | sed -e 's/[\/&]/\\&/g')
GTM_NOSCRIPT_ESCAPED=$(echo "$GTM_NOSCRIPT" | sed -e 's/[\/&]/\\&/g')

# Perform the replacements
sed -i "s~<!-- Google Tag Manager -->~$GTM_SCRIPT_ESCAPED~" $INDEX_FILE
sed -i "s~<!-- Google Tag Manager (noscript) -->~$GTM_NOSCRIPT_ESCAPED~" $INDEX_FILE
fi

## Gzip after replacements
Expand Down

0 comments on commit f07668a

Please sign in to comment.