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 18 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 @@ -49,6 +49,12 @@ public class SimpleProcessorService implements ProcessorService {
@Value("${app.solr.batch-size:250}")
private Integer batchSize;

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

public String getGoogleAnalytics() {
return googleAnalytics;
}
qtamu marked this conversation as resolved.
Show resolved Hide resolved
@Autowired
private SimpMessagingTemplate simpMessagingTemplate;

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/edu/tamu/sage/ui/controller/ViewController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import edu.tamu.sage.service.SimpleProcessorService;

// TODO: this most likely can move into the framework
@Controller
public class ViewController {

// private final SimpleProcessorService simpleprocessorService;
private static SimpleProcessorService simpleProcessorService;


public ViewController(SimpleProcessorService simpleProcessorService) {
ViewController.simpleProcessorService = simpleProcessorService;
}

qtamu marked this conversation as resolved.
Show resolved Hide resolved
@RequestMapping("/")
public ModelAndView view(HttpServletRequest request) {
return index(request);
Expand All @@ -18,6 +27,7 @@ public ModelAndView view(HttpServletRequest request) {
public static ModelAndView index(HttpServletRequest request) {
ModelAndView index = new ModelAndView("index");
index.addObject("base", request.getServletContext().getContextPath());
index.addObject("googleAnalytics", simpleProcessorService.getGoogleAnalytics());
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