Skip to content

Commit d821bea

Browse files
author
Rehan van der Merwe
authored
feat: track demo page and add demo banner #4 (#11)
1 parent fd12319 commit d821bea

File tree

13 files changed

+7979
-1124
lines changed

13 files changed

+7979
-1124
lines changed

docs/API.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ const domain: Domain = { ... }
370370
| <code><a href="#serverless-website-analytics.Domain.property.certificate">certificate</a></code> | <code>aws-cdk-lib.aws_certificatemanager.ICertificate</code> | The certificate to use for the domain. |
371371
| <code><a href="#serverless-website-analytics.Domain.property.name">name</a></code> | <code>string</code> | Name of the domain to use for the site, example: `serverless-website-analytics.com`. |
372372
| <code><a href="#serverless-website-analytics.Domain.property.hostedZone">hostedZone</a></code> | <code>aws-cdk-lib.aws_route53.IHostedZone</code> | Optional, if not specified then no DNS records will be created. |
373+
| <code><a href="#serverless-website-analytics.Domain.property.trackOwnDomain">trackOwnDomain</a></code> | <code>boolean</code> | Optional, if specified, it adds tracking to the dashboard. |
373374

374375
---
375376

@@ -417,6 +418,21 @@ the domain, for example: `{auth.cognito.loginSubDomain}.{domain.name}`.
417418

418419
---
419420

421+
##### `trackOwnDomain`<sup>Optional</sup> <a name="trackOwnDomain" id="serverless-website-analytics.Domain.property.trackOwnDomain"></a>
422+
423+
```typescript
424+
public readonly trackOwnDomain: boolean;
425+
```
426+
427+
- *Type:* boolean
428+
429+
Optional, if specified, it adds tracking to the dashboard.
430+
431+
This is useful if you want to see the usage of
432+
the serverless-website-analytics dashboard page.
433+
434+
---
435+
420436
### SwaAuth <a name="SwaAuth" id="serverless-website-analytics.SwaAuth"></a>
421437

422438
The auth configuration which defaults to none.
@@ -628,6 +644,7 @@ const swaProps: SwaProps = { ... }
628644
| <code><a href="#serverless-website-analytics.SwaProps.property.sites">sites</a></code> | <code>string[]</code> | The list of allowed sites. |
629645
| <code><a href="#serverless-website-analytics.SwaProps.property.auth">auth</a></code> | <code><a href="#serverless-website-analytics.SwaAuth">SwaAuth</a></code> | The auth configuration which defaults to none. |
630646
| <code><a href="#serverless-website-analytics.SwaProps.property.domain">domain</a></code> | <code><a href="#serverless-website-analytics.Domain">Domain</a></code> | If specified, it will create the CloudFront and Cognito resources at the specified domain and optionally create the DNS records in the specified Route53 hosted zone. |
647+
| <code><a href="#serverless-website-analytics.SwaProps.property.isDemoPage">isDemoPage</a></code> | <code>boolean</code> | If specified, adds the banner at the top of the page linking back to the open source project. |
631648

632649
---
633650

@@ -720,5 +737,17 @@ Cognito(`auth.us-east-1.amazoncognito.com`) domains. You can read the website UR
720737

721738
---
722739

740+
##### `isDemoPage`<sup>Optional</sup> <a name="isDemoPage" id="serverless-website-analytics.SwaProps.property.isDemoPage"></a>
741+
742+
```typescript
743+
public readonly isDemoPage: boolean;
744+
```
745+
746+
- *Type:* boolean
747+
748+
If specified, adds the banner at the top of the page linking back to the open source project.
749+
750+
---
751+
723752

724753

src/backend.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ export function backend(
107107
...defaultNodeJsFuncOpt,
108108
memorySize: 1024,
109109
timeout: Duration.seconds(frontLambdaTimeOut),
110-
environment: env,
110+
environment: {
111+
...env,
112+
TRACK_OWN_DOMAIN: props?.domain?.trackOwnDomain ? 'true' : 'false',
113+
IS_DEMO_PAGE: props.isDemoPage ? 'true' : 'false',
114+
},
111115
reservedConcurrentExecutions: 100,
112116
});
113117
apiFrontLambda.addToRolePolicy(

src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ export interface Domain {
9090
* the domain, for example: `{auth.cognito.loginSubDomain}.{domain.name}`.
9191
*/
9292
readonly hostedZone?: route53.IHostedZone;
93+
94+
/**
95+
* Optional, if specified, it adds tracking to the dashboard. This is useful if you want to see the usage of
96+
* the serverless-website-analytics dashboard page.
97+
*/
98+
readonly trackOwnDomain?: boolean;
9399
}
94100

95101
export interface SwaProps {
@@ -131,6 +137,11 @@ export interface SwaProps {
131137
* Cognito(`auth.us-east-1.amazoncognito.com`) domains. You can read the website URL from the stack output.
132138
*/
133139
readonly domain?: Domain;
140+
141+
/**
142+
* If specified, adds the banner at the top of the page linking back to the open source project.
143+
*/
144+
readonly isDemoPage?: boolean;
134145
}
135146

136147
export class Swa extends Construct {
@@ -148,6 +159,11 @@ export class Swa extends Construct {
148159
}
149160
}
150161

162+
if (props?.domain?.trackOwnDomain) {
163+
props.sites.push(props.domain.name);
164+
props.allowedOrigins.push(`https://${props.domain.name}`);
165+
}
166+
151167
const authProps = auth(scope, name, props);
152168
const backendAnalyticsProps = backendAnalytics(scope, name, props);
153169
const backendProps = backend(scope, name, props, authProps, backendAnalyticsProps);

src/src/backend/api-front/environment.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,37 @@ export class LambdaEnvironment {
1818
static COGNITO_CLIENT_ID: string | undefined;
1919
static COGNITO_HOSTED_UI_URL: string | undefined;
2020

21+
static TRACK_OWN_DOMAIN: boolean;
22+
static IS_DEMO_PAGE: boolean;
23+
2124
static init() {
2225
const schema = z.object({
2326
AWS_REGION: z.string(),
2427
ENVIRONMENT: z.string(),
2528
VERSION: z.string(),
26-
TIMEOUT: z.string(),
29+
TIMEOUT: z.string().transform((v) => Number(v)),
2730
LOG_LEVEL: z.string(),
28-
ENRICH_RETURNED_ERRORS: z.string(),
31+
ENRICH_RETURNED_ERRORS: z.string().transform((v) => Boolean(v) && v !== 'false'),
2932

3033
ANALYTICS_BUCKET: z.string(),
3134
ANALYTICS_GLUE_DB_NAME: z.string(),
32-
SITES: z.string(),
35+
SITES: z.string().transform((v) => JSON.parse(v) as string[]),
3336

3437
COGNITO_USER_POOL_ID: z.string().optional(),
3538
COGNITO_CLIENT_ID: z.string().optional(),
3639
COGNITO_HOSTED_UI_URL: z.string().optional(),
37-
});
3840

41+
TRACK_OWN_DOMAIN: z
42+
.string()
43+
.optional()
44+
.transform((v) => Boolean(v) && v !== 'false'),
45+
IS_DEMO_PAGE: z
46+
.string()
47+
.optional()
48+
.transform((v) => Boolean(v) && v !== 'false'),
49+
});
3950
const parsed = schema.safeParse(process.env);
51+
4052
if (!parsed.success) {
4153
console.error(parsed.error);
4254
throw new Error('Environment Variable Parse Error');
@@ -45,13 +57,9 @@ export class LambdaEnvironment {
4557
for (const key in parsed.data) {
4658
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4759
// @ts-ignore we know this is safe
48-
this[key] = process.env[key];
60+
this[key] = parsed.data[key];
4961
}
5062

51-
this.TIMEOUT = Number(this.TIMEOUT);
5263
this.ANALYTICS_BUCKET_ATHENA_PATH = 's3://' + this.ANALYTICS_BUCKET + '/athena-results';
53-
this.ENRICH_RETURNED_ERRORS = Boolean(this.ENRICH_RETURNED_ERRORS);
54-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
55-
this.SITES = JSON.parse(process.env.SITES!) as string[];
5664
}
5765
}

src/src/backend/api-front/routes/env/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { LambdaEnvironment } from '@backend/api-front/environment';
44

55
const FrontendEnvironmentSchema = z.object({
66
cognitoLoginUrl: z.string().optional(),
7+
trackOwnDomain: z.boolean().optional(),
8+
isDemoPage: z.boolean().optional(),
79
});
810
export type FrontendEnvironment = z.infer<typeof FrontendEnvironmentSchema>;
911

@@ -20,6 +22,8 @@ export function getFrontendEnvironment(trpcInstance: TrpcInstance) {
2022
}
2123
return {
2224
cognitoLoginUrl,
25+
trackOwnDomain: LambdaEnvironment.TRACK_OWN_DOMAIN,
26+
isDemoPage: LambdaEnvironment.IS_DEMO_PAGE,
2327
};
2428
});
2529
}

src/src/frontend/.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
VITE_FRONTEND_URL=http://localhost:3001/api
2+
VITE_INGEST_URL=http://localhost:3000

src/src/frontend/.env.production

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
VITE_FRONTEND_URL=/api
2+
VITE_INGEST_URL=

0 commit comments

Comments
 (0)