Skip to content

Commit

Permalink
add google analytics (#397)
Browse files Browse the repository at this point in the history
  • Loading branch information
longshuicy authored May 30, 2024
1 parent 9ac88d5 commit 0453009
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Gas facility damage analysis documentation and example notebook [#387](https://github.com/IN-CORE/incore-docs/issues/387)
- Traffic flow recovery analysis documentation and example notebook [#389](https://github.com/IN-CORE/incore-docs/issues/389)
- Social vulnerability score analysis documentation and example notebook [#392](https://github.com/IN-CORE/incore-docs/issues/392)
- Google Analytics to the api sphinx documentation [#396](https://github.com/IN-CORE/incore-docs/issues/396)

## [4.11.0] - 2024-04-30

Expand Down
1 change: 1 addition & 0 deletions restapi/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ RUN apt-get -qq update && apt-get -qq install git
RUN mkdir /usr/share/nginx/html/doc && \
mkdir /usr/share/nginx/html/doc/api
COPY index.html /usr/share/nginx/html/doc/api/.
COPY config /usr/share/nginx/html/doc/api/config
COPY api.conf /etc/nginx/conf.d/default.conf

3 changes: 3 additions & 0 deletions restapi/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"GA_KEY": "Test-Google-Analytics-Key-Replace-Me"
}
31 changes: 31 additions & 0 deletions restapi/config/googleAnalytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// analytics.js
(function() {
// Fetch the runtime configuration
fetch('config/config.json')
.then(response => {
if (!response.ok) {
throw new Error('Configuration file not found');
}
return response.json();
})
.then(config => {
if (!config.GA_KEY) {
throw new Error('GA_KEY is missing in the configuration');
}

// Create the script tag for Google Tag Manager
const scriptTag = document.createElement('script');
scriptTag.async = true;
scriptTag.src = `https://www.googletagmanager.com/gtag/js?id=${config.GA_KEY}`;
document.head.appendChild(scriptTag);

// Initialize Google Analytics
window.dataLayer = window.dataLayer || [];

function gtag() { dataLayer.push(arguments); }

gtag('js', new Date());
gtag('config', config.GA_KEY);
})
.catch(error => console.warn('GA setup skipped:', error.message));
})();
11 changes: 11 additions & 0 deletions restapi/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
// Fetch and execute the analytics script
fetch('config/googleAnalytics.js')
.then(response => response.text())
.then(scriptContent => {
const scriptTag = document.createElement('script');
scriptTag.textContent = scriptContent;
document.head.appendChild(scriptTag);
})
.catch(error => console.error('Failed to load analytics script:', error));
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="IN-CORE RESTAPI Swagger Documentation - Data, Hazard & Fragility Services">
Expand Down

0 comments on commit 0453009

Please sign in to comment.