Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(landing): track with google analytics if $GOOGLE_ANALYTICS is set #764

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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}}
22 changes: 22 additions & 0 deletions packages/landing/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `<script async src="https://www.googletagmanager.com/gtag/js?id=${trackingId}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '${trackingId}');
</script>
</head>`;

// Put the tracking at the bottom of the index.html's head tag
const outputHtml = rawIndexHtml.toString().replace('</head>', 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');
Expand Down