Skip to content

Commit

Permalink
adding analytics to experiment html page
Browse files Browse the repository at this point in the history
  • Loading branch information
ojas1 committed Oct 7, 2020
1 parent c1da2cc commit 661b4f9
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 8 deletions.
4 changes: 4 additions & 0 deletions analytics/bodySnippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W59SWTR"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
18 changes: 18 additions & 0 deletions analytics/headSnippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--Google Tag Manager-->
<script>
window.dataLayer = [{
'labName': '{{LAB_NAME}}',
'discipline': '{{BROAD_AREA}}',
'college': '{{COLLEGE_NAME}}',
'phase': '{{PHASE}}',
'expName': '{{EXP_NAME}}',
'expShortName': '{{EXP_SHORT_NAME}}'
}];

(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-W59SWTR');
</script>
<!--End Google Tag Manager-->
29 changes: 29 additions & 0 deletions analytics/insert-analytics.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
root=expbuilds
chmod -R u+rwx $root
head_snippet=$(tr -d '\n' < analytics/headSnippet)
body_snippet=$(tr -d '\n' < analytics/bodySnippet)
topic_map=$(tr -d '\n' < analytics/topic_config.json)
function insert_analytics_snippets () {
fn=$1

if [ $(grep -c "window.dataLayer" $fn) == 0 ]
then
sed -i "s@<head>@<head>${head_snippet//&/\\&}@" $fn
sed -i "s@<body>@<body>${body_snippet//&/\\&}@" $fn
else
echo "analytics script already present."
echo
fi
}
# get the list of html pages
pages=$(find $root -not -path "*/themes/*" -type f -name "*.html")

# insert snippets in all html pages
for p in $pages
do
#echo $p
insert_analytics_snippets $p
done

find $root -name "*.html" -exec sed -i "s@TOPIC_MAP@$topic_map@" {} \;
29 changes: 29 additions & 0 deletions analytics/insert-analytics.sh~
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
root=build/docs
chmod -R u+rwx $root
head_snippet=$(tr -d '\n' < analytics/headSnippet)
body_snippet=$(tr -d '\n' < analytics/bodySnippet)
topic_map=$(tr -d '\n' < analytics/topic_config.json)
function insert_analytics_snippets () {
fn=$1

if [ $(grep -c "window.dataLayer" $fn) == 0 ]
then
sed -i "s@<head>@<head>${head_snippet//&/\\&}@" $fn
sed -i "s@<body>@<body>${body_snippet//&/\\&}@" $fn
else
echo "analytics script already present."
echo
fi
}
# get the list of html pages
pages=$(find $root -not -path "*/themes/*" -type f -name "*.html")

# insert snippets in all html pages
for p in $pages
do
#echo $p
insert_analytics_snippets $p
done

find $root -name "*.html" -exec sed -i "s@TOPIC_MAP@$topic_map@" {} \;
56 changes: 48 additions & 8 deletions build_exp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,61 @@ cd exprepos/$reponame; git fetch --all; git checkout $tag; cd ../../
template=ui3template

## conversion

## Copy template files to the build directory of the experiment.

mkdir -p expbuilds/$EXP_SHORT_NAME
cp -r $template/* expbuilds/$EXP_SHORT_NAME

find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s/{{BROAD_AREA}}/$BROAD_AREA/g" {} +
find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s/{{LAB_NAME}}/$DISPLAY_LAB_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s/{{COLLEGE_NAME}}/$COLLEGE_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s/{{PHASE}}/$PHASE/g" {} +
find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s/{{EXP_NAME}}/$EXP_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s/{{EXP_SHORT_NAME}}/$EXP_SHORT_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s,{{BA_LINK}},$BROAD_AREA_LINK,g" {} +
find expbuilds/$EXP_SHORT_NAME/*.html -maxdepth 1 -type f -exec sed -i "s,{{LAB_LINK}},$LAB_LINK,g" {} +

## Copy content (simulations, and other files like markdown, images etc) from the source repo directory
## to the build directory and rename it to "round-template".

cp -r exprepos/$reponame expbuilds/$EXP_SHORT_NAME
mv expbuilds/$EXP_SHORT_NAME/$reponame expbuilds/$EXP_SHORT_NAME/round-template


## Insert analytics snippet in the "experiment" directory.
head_snippet=$(tr -d '\n' < analytics/headSnippet)
body_snippet=$(tr -d '\n' < analytics/bodySnippet)

function insert_analytics_snippets () {
fn=$1
bodytag=$(grep "<body" $fn)
if [ $(grep -c "window.dataLayer" $fn) == 0 ]
then
sed -i "s@<head>@<head>${head_snippet//&/\\&}@" $fn
sed -i "s@$bodytag@$bodytag${body_snippet//&/\\&}@" $fn
else
echo "analytics script already present."
echo
fi
}

pages=$(find expbuilds/$EXP_SHORT_NAME/round-template/experiment/ -type f -name "*.html")

echo $pages

# insert snippets in all html pages
for p in $pages
do
echo $p
insert_analytics_snippets $p
done


## Replace the placeholders for the data layer variables with actual values.

find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s/{{BROAD_AREA}}/$BROAD_AREA/g" {} +
find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s/{{LAB_NAME}}/$DISPLAY_LAB_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s/{{COLLEGE_NAME}}/$COLLEGE_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s/{{PHASE}}/$PHASE/g" {} +
find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s/{{EXP_NAME}}/$EXP_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s/{{EXP_SHORT_NAME}}/$EXP_SHORT_NAME/g" {} +
find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s,{{BA_LINK}},$BROAD_AREA_LINK,g" {} +
find expbuilds/$EXP_SHORT_NAME/ -type f -name "*.html" -exec sed -i "s,{{LAB_LINK}},$LAB_LINK,g" {} +


cp fixpointer.py expbuilds/$EXP_SHORT_NAME/round-template/experiment/simulation/
cd expbuilds/$EXP_SHORT_NAME/round-template/experiment/simulation/
python fixpointer.py
Expand Down

0 comments on commit 661b4f9

Please sign in to comment.