From e8939de949694878ed4773c196d55a09cf9efa72 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Thu, 11 Jun 2020 10:54:51 +1200 Subject: [PATCH] feat(landing): track with google analytics if $GOOGLE_ANALYTICS is set during deployment --- .github/workflows/push.yml | 4 +++- packages/landing/deploy.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index a6fab62fc..6f14a24af 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -58,6 +58,7 @@ jobs: env: ALB_CERTIFICATE_ARN: ${{secrets.ALB_CERTIFICATE_ARN}} CLOUDFRONT_CERTIFICATE_ARN: ${{secrets.CLOUDFRONT_CERTIFICATE_ARN}} + GOOGLE_ANALYTICS: ${{secrets.GOOGLE_ANALYTICS_NON_PROD}} - name: Publish NPM if: startsWith(github.ref, 'refs/tags/v') && github.repository == 'linz/basemaps' @@ -81,4 +82,5 @@ jobs: env: ALB_CERTIFICATE_ARN: ${{secrets.ALB_CERTIFICATE_ARN_PROD}} CLOUDFRONT_CERTIFICATE_ARN: ${{secrets.CLOUDFRONT_CERTIFICATE_ARN_PROD}} - NODE_ENV: 'production' + NODE_ENV: "production" + GOOGLE_ANALYTICS: ${{secrets.GOOGLE_ANALYTICS_PROD}} diff --git a/packages/landing/deploy.js b/packages/landing/deploy.js index e9684572b..6c883c03a 100644 --- a/packages/landing/deploy.js +++ b/packages/landing/deploy.js @@ -20,12 +20,34 @@ async function getHash(Bucket, Key) { return null; } +/** If $GOOGLE_ANALYTICS is set, insert the tracking code into the index.html */ +async function insertGoogleAnalytics() { + const trackingId = process.env['GOOGLE_ANALYTICS']; + if (trackingId == null || trackingId.trim() == '') return; + + const rawIndexHtml = await fs.readFile('./static/index.html'); + const trackingCode = ` + +`; + + // Put the tracking at the bottom of the index.html's head tag + const outputHtml = rawIndexHtml.toString().replace('', trackingCode); + await fs.writeFile('./static/index.html', outputHtml); +} + /** * Deploy the built s3 assets into the Edge bucket * * TODO there does not appear to be a easy way to do this with aws-cdk yet */ async function deploy() { + insertGoogleAnalytics(); // Since the bucket is generated inside of CDK lets look up the bucket name const stackInfo = await cf.describeStacks({ StackName: 'Edge' }).promise(); const bucket = stackInfo.Stacks[0].Outputs.find((f) => f.OutputKey == 'CloudFrontBucket');