Skip to content

Commit 1884ff5

Browse files
committed
feat(google-analytics): support multiple measurement ids
1 parent 513b8f5 commit 1884ff5

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

Diff for: .github/workflows/prod-build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ jobs:
195195
# If it's used on other domains (e.g. stage or dev builds), it's OK
196196
# because ultimately Google Analytics will filter it out since the
197197
# origin domain isn't what that account expects.
198-
BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID: UA-36116321-5
198+
BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID: UA-36116321-5,G-PWTK27XVWP
199199

200200
# This enables the MDN Plus
201201
REACT_APP_ENABLE_PLUS: true

Diff for: docs/envvars.md

+6
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ If set, the rendered HTML will have a Google Analytics snippet. For example, to
151151
test use: `export BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID=G-XXXXXXXX`. By default
152152
it's disabled (empty string).
153153

154+
For dual tagging (UA + GA4), multiple IDs can be separated by a comma:
155+
156+
```env
157+
export BUILD_GOOGLE_ANALYTICS_MEASUREMENT_ID=UA-00000000-0,G-XXXXXXXX
158+
```
159+
154160
### `BUILD_ALWAYS_ALLOW_ROBOTS`
155161

156162
**Default: `false`**

Diff for: tool/cli.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ program
867867
default: path.join(BUILD_OUT_ROOT, "static", "js", "gtag.js"),
868868
})
869869
.option(
870-
"--measurement-id <id>",
871-
"Google Analytics measurement ID (defaults to value of $GOOGLE_ANALYTICS_MEASUREMENT_ID)",
870+
"--measurement-id <id>[,<id>]",
871+
"Google Analytics measurement IDs (defaults to value of $GOOGLE_ANALYTICS_MEASUREMENT_ID)",
872872
{
873873
default: GOOGLE_ANALYTICS_MEASUREMENT_ID,
874874
}
@@ -877,15 +877,17 @@ program
877877
tryOrExit(
878878
async ({ options, logger }: GoogleAnalyticsCodeActionParameters) => {
879879
const { outfile, measurementId } = options;
880-
if (measurementId) {
880+
const measurementIds = measurementId.split(",").filter(Boolean);
881+
if (measurementIds.length) {
881882
const dntHelperCode = fs
882883
.readFileSync(
883884
new URL("mozilla.dnthelper.min.js", import.meta.url),
884885
"utf-8"
885886
)
886887
.trim();
887888

888-
const gaScriptURL = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(measurementId)}`;
889+
const firstMeasurementId = measurementIds.at(0);
890+
const gaScriptURL = `https://www.googletagmanager.com/gtag/js?id=${encodeURIComponent(firstMeasurementId)}`;
889891

890892
const code = `
891893
// Mozilla DNT Helper
@@ -895,7 +897,9 @@ if (Mozilla && !Mozilla.dntEnabled()) {
895897
window.dataLayer = window.dataLayer || [];
896898
function gtag(){dataLayer.push(arguments);}
897899
gtag('js', new Date());
898-
gtag('config', '${measurementId}', { 'anonymize_ip': true });
900+
${measurementIds
901+
.map((id) => `gtag('config', '${id}', { 'anonymize_ip': true });`)
902+
.join("\n ")}
899903
900904
var gaScript = document.createElement('script');
901905
gaScript.async = true;

0 commit comments

Comments
 (0)