Skip to content

Commit 4ce308e

Browse files
author
Michael Levin
committed
[Tech Debt] Enable DAP code injection for environments using NGINX SSI
1 parent 9863f57 commit 4ce308e

8 files changed

Lines changed: 105 additions & 55 deletions

File tree

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
FROM nginx
2-
COPY test_site Universal-Federated-Analytics-Min.js Federated.js.map /usr/share/nginx/html/
2+
ARG DAP_ENV='dev'
3+
ENV DAP_ENV=${DAP_ENV}
4+
COPY test_site Universal-Federated-Analytics-Min.js Federated.js.map /usr/share/nginx/html/
5+
COPY nginx-test.conf.template /etc/nginx/conf.d/
6+
RUN envsubst '${DAP_ENV}' < /etc/nginx/conf.d/nginx-test.conf.template > /etc/nginx/conf.d/default.conf

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,17 @@ npm run lint
131131

132132
#### Run integration tests
133133

134-
Start up the test site at http://localhost:8080/:
134+
Start up the test site at http://localhost:8080/ with one of the following:
135135

136136
```bash
137-
npm run test-site
137+
# Using the local version of the DAP code
138+
npm run test-site-dev
139+
140+
# Using the staging environment version of the DAP code
141+
npm run test-site-stg
142+
143+
# Using the production environment version of the DAP code
144+
npm run test-site-prd
138145
```
139146

140147
Then run the tests against the test site:

features/support/step_definitions/browser_steps.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ Then("there are no unexpected requests", function () {
6262
return (new URL(request.url)).host;
6363
});
6464

65-
const allowedURLs = ["localhost:8080", "www.googletagmanager.com", "www.google-analytics.com"];
66-
67-
if (process.env.DAP_ENV == 'production') {
68-
allowedURLs.push("dap.digitalgov.gov")
69-
} else if (process.env.DAP_ENV == 'staging') {
70-
allowedURLs.push("d3vtlq0ztv2u27.cloudfront.net")
71-
}
65+
const allowedURLs = [
66+
"localhost:8080",
67+
"d3vtlq0ztv2u27.cloudfront.net",
68+
"dap.digitalgov.gov",
69+
"www.googletagmanager.com",
70+
"www.google-analytics.com"
71+
];
7272

7373
requestURLs.forEach((requestURL) => {
7474
expect(allowedURLs).to.include(requestURL);

features/support/step_definitions/loading_steps.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ Given("DAP is configured with autotracking disabled", function () {
2727
});
2828

2929
When("I load the test site", async function () {
30-
await this.page.goto(`http://localhost:8080?${this.dapConfig.toQueryParams()}${process.env.DAP_ENV ? ('&testEnv=' + process.env.DAP_ENV) : ''}`);
30+
await this.page.goto(`http://localhost:8080?${this.dapConfig.toQueryParams()}`);
3131
});

nginx-test.conf.template

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
server_name localhost;
5+
6+
#access_log /var/log/nginx/host.access.log main;
7+
8+
location / {
9+
ssi on;
10+
set $dap_code_env '${DAP_ENV}';
11+
set $dap_code_location 'Universal-Federated-Analytics-Min.js';
12+
13+
if ($dap_code_env = 'stg') {
14+
set $dap_code_location 'https://d3vtlq0ztv2u27.cloudfront.net/Universal-Federated-Analytics-Min.js';
15+
}
16+
if ($dap_code_env = 'prd') {
17+
set $dap_code_location 'https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js';
18+
}
19+
20+
# Create the script tag to include in each page's header using the
21+
# dap_code_location which is defined above
22+
set $header_script_tag '
23+
<script>
24+
(function() {
25+
const queryParams = new URLSearchParams(window.location.search);
26+
const dapScriptTag = document.createElement("script");
27+
const dapCodeLocation = ""
28+
dapScriptTag.id = "_fed_an_ua_tag";
29+
dapScriptTag.async = true;
30+
dapScriptTag.src = "${dap_code_location}" + "?dapdev=true&" + queryParams.toString();
31+
document.head.appendChild(dapScriptTag);
32+
})();
33+
</script>
34+
';
35+
36+
root /usr/share/nginx/html;
37+
index index.html index.htm;
38+
}
39+
40+
#error_page 404 /404.html;
41+
42+
# redirect server error pages to the static page /50x.html
43+
#
44+
error_page 500 502 503 504 /50x.html;
45+
location = /50x.html {
46+
root /usr/share/nginx/html;
47+
}
48+
49+
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
50+
#
51+
#location ~ \.php$ {
52+
# proxy_pass http://127.0.0.1;
53+
#}
54+
55+
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
56+
#
57+
#location ~ \.php$ {
58+
# root html;
59+
# fastcgi_pass 127.0.0.1:9000;
60+
# fastcgi_index index.php;
61+
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
62+
# include fastcgi_params;
63+
#}
64+
65+
# deny access to .htaccess files, if Apache's document root
66+
# concurs with nginx's one
67+
#
68+
#location ~ /\.ht {
69+
# deny all;
70+
#}
71+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
"lint": "eslint Universal-Federated-Analytics.js",
1010
"cucumber": "cucumber-js",
1111
"cucumber:debug": "VERBOSE=true node --inspect-brk node_modules/@cucumber/cucumber/bin/cucumber-js",
12-
"test-site": "docker build -t dap-test-site . && docker run --rm -p 8080:80 --name dap-test-site dap-test-site"
12+
"test-site-dev": "docker build --no-cache --build-arg DAP_ENV='dev' -t dap-test-site-dev . && docker run --rm -p 8080:80 --name dap-test-site-dev dap-test-site-dev",
13+
"test-site-stg": "docker build --no-cache --build-arg DAP_ENV='stg' -t dap-test-site-stg . && docker run --rm -p 8080:80 --name dap-test-site-stg dap-test-site-stg",
14+
"test-site-prd": "docker build --no-cache --build-arg DAP_ENV='prd' -t dap-test-site-prd . && docker run --rm -p 8080:80 --name dap-test-site-prd dap-test-site-prd"
1315
},
1416
"repository": {
1517
"type": "git",

test_site/index.html

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55

6-
7-
<title>DAP test site</title>
8-
<script>
9-
(function() {
10-
const queryParams = new URLSearchParams(window.location.search);
11-
const dapScriptTag = document.createElement("script");
12-
let dapCodeLocation;
13-
dapScriptTag.id = "_fed_an_ua_tag";
14-
dapScriptTag.async = true;
15-
16-
if (queryParams.get('testEnv') == 'production') {
17-
queryParams.delete('testEnv');
18-
dapCodeLocation = 'https://dap.digitalgov.gov/Universal-Federated-Analytics-Min.js';
19-
} else if (queryParams.get('testEnv') == 'staging') {
20-
queryParams.delete('testEnv');
21-
dapCodeLocation = 'https://d3vtlq0ztv2u27.cloudfront.net/Universal-Federated-Analytics-Min.js';
22-
} else {
23-
dapCodeLocation = 'Universal-Federated-Analytics-Min.js';
24-
}
25-
26-
dapScriptTag.src = `${dapCodeLocation}?dapdev=true&${queryParams.toString()}`
27-
document.head.appendChild(dapScriptTag);
28-
})();
29-
</script>
6+
<title>DAP test site</title>
7+
<!-- This is populated by NGINX SSI -->
8+
<!--#echo var="header_script_tag" encoding="none"-->
309
</head>
3110

3211
<body>

test_site/youtube.html

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55

6-
7-
<title>DAP test site - YouTube tracking</title>
8-
<script>
9-
(function() {
10-
const queryParams = new URLSearchParams(window.location.search);
11-
if (!queryParams.has("agency")) {
12-
queryParams.set("agency", "GSA");
13-
}
14-
const dapScriptTag = document.createElement("script");
15-
dapScriptTag.id = "_fed_an_ua_tag";
16-
dapScriptTag.async = true;
17-
dapScriptTag.src = `Universal-Federated-Analytics-Min.js?dapdev=true&${queryParams.toString()}`;
18-
document.head.appendChild(dapScriptTag);
19-
})();
20-
21-
</script>
6+
<title>DAP test site - YouTube tracking</title>
7+
<!-- This is populated by NGINX SSI -->
8+
<!--#echo var="header_script_tag" encoding="none"-->
229
</head>
2310

2411
<body>
@@ -34,4 +21,4 @@
3421
title="YouTube video player" frameborder="0" allowfullscreen></iframe>
3522
</section>
3623
</body>
37-
</html>
24+
</html>

0 commit comments

Comments
 (0)