3

I have research on this and I can implement google analytics on a single java page. However, the portal have multiple pages and I cannot repeat it for every single page. For html or php sites, we can easily do it by copying some code into header or footer. What is the best way to do it on Java project ?

Attached is my project structure: enter image description here

And here is how the header.jsp looks like enter image description here Thank you,

user1314404
  • 1,253
  • 3
  • 22
  • 51

2 Answers2

0

create a common jsp page for your google analysis and import this jsp to your template jsp using c:import you can dynamically change the attribute as per your requirement

c:import example

:display.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="Chaitanya"/>
<c:out value="BeginnersBook.com" />
<c:out value="This is just a String" />

index.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> JSTL c:import Tag Example</title>
</head>
<body>
<c:import var="mydata" url="/display.jsp"/>
<c:out value="${mydata}"/>
</body>
</html>

Here is a nice post Import content to your Web site from IBM

Bhushan Uniyal
  • 5,575
  • 2
  • 22
  • 45
0

we need to add script code in the header of every page. and you already have a separate header.jsp file which is included in every page.

so, you can add code in the just header.jsp

If you are using Google analytics, then

<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"> 
</script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-X');
</script>
</head>

If you are using a Google tag manager, then

<head>
<!-- Google Tag Manager -->
<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-XXXXXXX');</script>
<!-- End Google Tag Manager -->
</head>
krishna
  • 11
  • 4