Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 557 google analytics #558

Merged
merged 21 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ COPY ./package.json ./package.json
USER root
COPY ./build/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh

# RUN chmod +x ./build/docker-entrypoint.sh
RUN chmod ugo+rx /usr/local/bin/docker-entrypoint.sh

# Assign file permissions.
Expand All @@ -65,15 +64,6 @@ RUN echo $NPM_REGISTRY && \
# Copy in your index.html file and modify it before the mvn package command
COPY ./src/main/resources/templates/index.html $SOURCE_DIR/src/main/resources/templates/index.html

RUN ga4=$(cat ./build/Ga4.txt) && \
gtm=$(cat ./build/Gtm.txt) && \
ga4_one_line=$(echo "$ga4" | tr -d '\n') && \
gtm_one_line=$(echo "$gtm" | tr -d '\n') && \
ga4_escaped=$(echo "$ga4_one_line" | sed -e 's/[\/&]/\\&/g') && \
gtm_escaped=$(echo "$gtm_one_line" | sed -e 's/[\/&]/\\&/g') && \
sed -i "s#<!--google Analytics Tag -->#${ga4_escaped}#g" $SOURCE_DIR/src/main/resources/templates/index.html && \
sed -i "s#<!-- Google Tag Manager (noscript) -->#${gtm_escaped}#g" $SOURCE_DIR/src/main/resources/templates/index.html

# Build.
RUN mvn package -Pjar -DskipTests

Expand Down Expand Up @@ -110,12 +100,7 @@ USER $USER_NAME
# Copy over the built artifact and library from the maven image.
COPY --from=maven $SOURCE_DIR/target/ROOT.jar ./sage.jar
COPY --from=maven $SOURCE_DIR/target/libs ./libs
COPY --from=maven $SOURCE_DIR/build/Ga4.txt ./build/Ga4.txt
COPY --from=maven $SOURCE_DIR/build/Gtm.txt ./build/Gtm.txt
COPY --from=maven $SOURCE_DIR/src/main/resources/templates/index.html ./src/main/resources/templates/index.html

# Make sure the user has the necessary permissions on index.html
RUN chown $USER_NAME:$USER_NAME ./src/main/resources/templates/index.html && chmod u+rw ./src/main/resources/templates/index.html
COPY --from=maven $SOURCE_DIR/build/docker-entrypoint.sh ./build/docker-entrypoint.sh

ENV AUTH_STRATEGY=weaverAuth

Expand Down
5 changes: 0 additions & 5 deletions build/Ga4.txt

This file was deleted.

2 changes: 0 additions & 2 deletions build/Gtm.txt

This file was deleted.

26 changes: 1 addition & 25 deletions build/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,7 @@ envsubst < /usr/local/app/templates/appConfig.js.template > appConfig.js
echo "Done"
head -n 20 appConfig.js

echo "Done docker-entrypoint..."

### Below is the injecting script that will inject the txt files into the placeholder###
### into the index.html file incase you do not want it to be built within the Docker image###

# Specify the directories containing the JavaScript and HTML files
# jsfiles_dir="./build/"
# htmlfiles_dir="./src/main/resources/templates/"

# Encode the scripts
# ga4=$(cat "${jsfiles_dir}Ga4.txt")
# gtm=$(cat "${jsfiles_dir}Gtm.txt")

# Remove any newline characters
# ga4_one_line=$(echo "$ga4" | tr -d '\n')
# gtm_one_line=$(echo "$gtm" | tr -d '\n')

# #escape the special characters for sed commands
# ga4_escaped=$(echo "$ga4_one_line" | sed -e 's/[\/&]/\\&/g')
# gtm_escaped=$(echo "$gtm_one_line" | sed -e 's/[\/&]/\\&/g')

# Inject the scripts
# sed -i "s#<!--google Analytics Tag -->#${ga4_escaped}#g" "${htmlfiles_dir}index.html"
# sed -i "s#<!-- Google Tag Manager (noscript) -->#${gtm_escaped}#g" "${htmlfiles_dir}index.html"

# chown -R $USER_NAME:$USER_NAME ./src/main/resources/templates && chmod -R u+rw ./src/main/resources/templates
echo "Done docker-entrypoint..."

exec "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.error.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

// TODO: this most likely can move into the framework
@RestController
@Controller
public class Html5ModeErrorController implements ErrorController {

private static final String PATH = "/error";

@Autowired
private ViewController viewController;

public String getErrorPath() {
return PATH;
}
Expand All @@ -23,7 +27,7 @@ public ModelAndView error(HttpServletRequest request, HttpServletResponse respon
if (request.getHeader("X-Requested-With") == null) {
response.setStatus(HttpServletResponse.SC_OK);
}
return ViewController.index(request);
return viewController.index(request);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
Expand All @@ -10,14 +11,18 @@
@Controller
public class ViewController {

@Value("${app.googleTag}")
private String googleAnalytics;

@RequestMapping("/")
public ModelAndView view(HttpServletRequest request) {
return index(request);
}

public static ModelAndView index(HttpServletRequest request) {
public ModelAndView index(HttpServletRequest request) {
ModelAndView index = new ModelAndView("index");
index.addObject("base", request.getServletContext().getContextPath());
index.addObject("googleAnalytics", googleAnalytics);
return index;
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ info:
app:
url: http://localhost:${server.port}${server.servlet.context-path}

googleTag: GTM-5ZRSWRW
# edu.tamu.weaver.email.config.WeaverEmailConfig
email:
host: relay.tamu.edu
Expand All @@ -119,6 +120,7 @@ app:
cvcache:
duration: 3600000


# edu.tamu.weaver.auth.service.UserCredentialsService
authority.admins:
security:
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/gh-pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ <h1 class="display-4">SAGE</h1>
</body>

</html>

14 changes: 12 additions & 2 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@

<head>
<!-- Google Tag Manager -->
<!--google Analytics Tag -->
<script th:inline="javascript">
(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', /*[[${googleAnalytics}]]*/ 'YOUR_DEFAULT_VALUE');
</script>
<!-- End Google Tag Manager -->
<script type="text/javascript" th:inline="javascript">
/*<![CDATA[*/
Expand Down Expand Up @@ -32,7 +41,8 @@
<body ng-controller="AppLoginController" class="wvr-components-loading">

<!-- Google Tag Manager (noscript) -->

<noscript><iframe th:src="@{'https://www.googletagmanager.com/ns.html?id=' + ${googleAnalytics}}" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->

<main>
Expand Down