-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration to Google Analytics 4 (#471)
- Loading branch information
1 parent
811034b
commit f336643
Showing
29 changed files
with
487 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ name: 'Alpha Build' | |
|
||
on: | ||
push: | ||
branches: [v4190/release] | ||
branches: [v4192/ga4] | ||
|
||
jobs: | ||
build: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -615,15 +615,6 @@ db().then(() => { | |
} | ||
}); | ||
|
||
// app.get('/api/test', async (req, res) => { | ||
// await sendSelfHostWelcome( | ||
// '[email protected]', | ||
// 'Antoine J', | ||
// 'BLAH-BLAH-BLAH' | ||
// ); | ||
// res.send('done'); | ||
// }); | ||
|
||
setupSentryErrorHandler(app); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference?client_type=gtag#payload_post_body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import config from './../config.js'; | ||
import request from 'request'; | ||
import chalkTemplate from 'chalk-template'; | ||
|
||
export function trackPurchase( | ||
/** | ||
* Uniquely identifies a user instance of a web client | ||
*/ | ||
clientId: string, | ||
/** | ||
* A unique identifier for a user, cross device and platform | ||
*/ | ||
userId: string, | ||
transactionId: string, | ||
productId: string, | ||
productName: string, | ||
quantity: number, | ||
currency: string, | ||
value: number | ||
) { | ||
const measurementId = config.GA4_MEASUREMENT_ID; | ||
const secret = config.GA4_SECRET; | ||
|
||
if (!measurementId || !secret) { | ||
console.log(chalkTemplate`{red GA4 not configured}`); | ||
return; | ||
} | ||
|
||
type EventPayload = { | ||
/** | ||
* Uniquely identifies a user instance of a web client | ||
*/ | ||
client_id: string; | ||
/** | ||
* A unique identifier for a user, cross device and platform | ||
*/ | ||
user_id?: string; | ||
events: PurchaseEvent[]; | ||
}; | ||
|
||
type PurchaseEvent = { | ||
name: string; | ||
params: PurchaseEventParams; | ||
}; | ||
|
||
type PurchaseEventParams = { | ||
currency?: string; | ||
value?: number; | ||
transaction_id: string; | ||
items: Item[]; | ||
}; | ||
|
||
type Item = { | ||
item_id: string; | ||
item_name: string; | ||
quantity: number; | ||
}; | ||
|
||
const payload: EventPayload = { | ||
client_id: clientId, | ||
user_id: userId, | ||
events: [ | ||
{ | ||
name: 'purchase', | ||
params: { | ||
transaction_id: transactionId, | ||
currency: currency, | ||
value: value, | ||
items: [ | ||
{ item_id: productId, item_name: productName, quantity: quantity }, | ||
], | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
console.log('Sending to GA: ', JSON.stringify(payload, null, 2)); | ||
|
||
request.post( | ||
{ | ||
url: | ||
'https://www.google-analytics.com/mp/collect?api_secret=' + | ||
secret + | ||
'&measurement_id=' + | ||
measurementId, | ||
body: JSON.stringify(payload), | ||
}, | ||
function (err, res) { | ||
console.log('Res: ', res.body); | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log('GA4 event sent successfully'); | ||
} | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.