Skip to content

Commit

Permalink
add support for (optional) GA4
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Jun 8, 2024
1 parent 43a7431 commit 5b55d53
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"generate-license-file": "^1.3.0",
"multer": "^1.4.5-lts.1",
"pagasa-archiver": "^1.3.1",
"pagasa-parser": "^2.2.4"
"pagasa-parser": "^2.2.4",
"string-replace-middleware": "^1.1.0"
},
"devDependencies": {
"@types/bunyan": "^1.8.8",
Expand Down
23 changes: 23 additions & 0 deletions backend/src/PagasaParserWeb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {FormatWikipediaEndpoint} from "./api/v1/FormatWikipediaEndpoint";
import {MetaLicensesEndpoint} from "./api/v1/MetaLicensesEndpoint";
import {BulletinGetEndpoint} from "./api/v1/BulletinGetEndpoint";
import {FormatSignalsEndpoint} from "./api/v1/FormatSignalsEndpoint";
import {stringReplace} from "string-replace-middleware";

// eslint-disable-next-line @typescript-eslint/no-var-requires
const packageInfo = require("../package.json");
Expand Down Expand Up @@ -77,6 +78,28 @@ export class PagasaParserWeb {
next();
});

// Middleware for Google Analytics
if ( process.env.PPW_GA4_GTAG ) {
if ( !/^G-[A-Z0-9]+/.test(process.env.PPW_GA4_GTAG) ) {
this.log.warn("Google Analytics GA4 measurement ID is not valid.");
} else {
this.app.use( stringReplace( {
"<!-- PPW_DYNAMIC -->": (req) => {
const dnt = req.header("dnt");
if (dnt != null && dnt !== "0") {
return "";
}

return `<script async src="https://www.googletagmanager.com/gtag/js?id=${
process.env.PPW_GA4_GTAG
}"></script><script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '${
process.env.PPW_GA4_GTAG
}');</script>`;
}
} ) );
}
}

// Static
this.app.use("/", express.static(path.join(__dirname, "..", "static")));

Expand Down
2 changes: 2 additions & 0 deletions backend/static/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<script src="js/pkg.react.js" async></script>
<script src="js/pkg.bootstrap.js" async></script>
<script src="js/pkg.misc.js" async></script>

<!-- PPW_DYNAMIC -->
</head>
<body>
<noscript>
Expand Down
2 changes: 2 additions & 0 deletions backend/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/styles/atom-one-dark.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.3.1/highlight.min.js"></script>

<!-- PPW_DYNAMIC -->
</head>
<body>
<header class="text-white">
Expand Down
35 changes: 34 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5b55d53

Please sign in to comment.