Skip to content

Commit

Permalink
adjust index with a google tag
Browse files Browse the repository at this point in the history
  • Loading branch information
endersonmenezes committed Jan 31, 2024
1 parent 3ee7312 commit 1f7d40f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/gh-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ jobs:
done
cd ..
# Adjust index.html files
files=$(find google-colab -name "index.html")
for file in $files; do
./scripts/adjust-index.sh $file
done
# Checkout on branch gh-pages-* depending on branch
git checkout ${{ steps.branch_check.outputs.env_name }}
Expand Down
35 changes: 35 additions & 0 deletions scripts/adjust-index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# Define the file
file=$1
if [ -z "$file" ]; then
echo "Usage: $0 <file>"
exit 1
fi

# Define the new code to be inserted
new_code='<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-HT7G6WDWHT"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('"'"'js'"'"', new Date());
gtag('"'"'config'"'"', '"'"'G-HT7G6WDWHT'"'"');
</script>'

# Create a temporary file
temp_file=$(mktemp)

# Delete the code exacly: <google-codelab-analytics gaid="UA-49880327-14" ga4id=""></google-codelab-analytics>
sed '/<google-codelab-analytics gaid="UA-49880327-14" ga4id=""><\/google-codelab-analytics>/d' "$file" > "$temp_file"

# Delete the attributes codelab-ga4id and codelab-gaid from google-codelab tag
sed -i 's/codelab-ga4id="[^"]*"//g' "$temp_file"
sed -i 's/codelab-gaid="[^"]*"//g' "$temp_file"

# Insert the new code after the <head> tag
awk -v ncode="$new_code" '/<head>/ {print; print ncode; next} 1' "$temp_file" > "$file"

# Remove the temporary file
rm "$temp_file"

0 comments on commit 1f7d40f

Please sign in to comment.