Skip to content

Commit

Permalink
Merge pull request #96 from thematters/fix/ga4
Browse files Browse the repository at this point in the history
fix(ga4):  add new events to handle ga4 data processing delay of 24-48 hours
  • Loading branch information
gary02 authored Nov 21, 2023
2 parents 82df51e + 30a1929 commit 0185d4c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
51 changes: 51 additions & 0 deletions bin/query-ga4-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { BetaAnalyticsDataClient } from "@google-analytics/data";

const propertyId = process.env.MATTERS_GA4_PROPERTY_ID;
const projectId = process.env.MATTERS_GA4_PROJECT_ID;
const clientEmail = process.env.MATTERS_GA4_CLIENT_EMAIL;
const privateKey = process.env.MATTERS_GA4_PRIVATE_KEY || "";

const main = async () => {
const startDate = "2023-11-21";
const endDate = "2023-11-21";
const client = new BetaAnalyticsDataClient({
projectId,
credentials: {
client_email: clientEmail,
private_key: privateKey.replace(/\\n/g, "\n"),
},
});
const [response] = await client.runReport({
property: `properties/${propertyId}`,
dateRanges: [
{
startDate,
endDate,
},
],
dimensions: [
{
name: "pagePath",
},
],
dimensionFilter: {
filter: {
fieldName: "pagePath",
stringFilter: {
matchType: "BEGINS_WITH",
value: "/@hi176/464031",
},
},
},
metrics: [
{
name: "totalUsers",
//name: 'activeUsers',
},
],
returnPropertyQuota: true,
});
console.dir(response.rows, { depth: null });
};

main();
38 changes: 37 additions & 1 deletion deployment/sync-ga-data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ Resources:
CronEvent2:
Type: "AWS::Events::Rule"
Properties:
ScheduleExpression: "cron(0 20 * * ? *)"
ScheduleExpression: "rate(30 minutes)"
Targets:
- Arn: !GetAtt Lambda.Arn
Id: CronEvent2LambdaTarget
Expand All @@ -143,3 +143,39 @@ Resources:
FunctionName: !Ref Lambda
Principal: events.amazonaws.com
SourceArn: !GetAtt CronEvent2.Arn
CronEvent3:
Type: "AWS::Events::Rule"
Properties:
ScheduleExpression: "rate(1 hour)"
Targets:
- Arn: !GetAtt Lambda.Arn
Id: CronEvent2LambdaTarget
Input: |
{
"type": "2 days ago"
}
CronEvent3Permission:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !Ref Lambda
Principal: events.amazonaws.com
SourceArn: !GetAtt CronEvent3.Arn
CronEvent4:
Type: "AWS::Events::Rule"
Properties:
ScheduleExpression: "rate(1 hour)"
Targets:
- Arn: !GetAtt Lambda.Arn
Id: CronEvent2LambdaTarget
Input: |
{
"type": "3 days ago"
}
CronEvent4Permission:
Type: "AWS::Lambda::Permission"
Properties:
Action: "lambda:InvokeFunction"
FunctionName: !Ref Lambda
Principal: events.amazonaws.com
SourceArn: !GetAtt CronEvent4.Arn
8 changes: 6 additions & 2 deletions handlers/sync-ga4-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ import {
// AWS EventBridge can configure the input event sent to Lambda,
// see https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-transform-target-input.html for info.
type Event = {
type: "today" | "yesterday";
type: "today" | "yesterday" | "2 days ago" | "3 days ago";
};

const getDate = (type: "today" | "yesterday") => {
const getDate = (type: Event["type"]) => {
const date = new Date();
if (type === "yesterday") {
date.setDate(date.getDate() - 1);
} else if (type === "2 days ago") {
date.setDate(date.getDate() - 2);
} else if (type === "3 days ago") {
date.setDate(date.getDate() - 3);
}
return getLocalDateString(date);
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lambda-handlers-image",
"version": "0.8.0",
"version": "0.8.1",
"private": true,
"type": "module",
"scripts": {
Expand Down

0 comments on commit 0185d4c

Please sign in to comment.