| 74 | }; |
| 75 | |
| 76 | const patchGtag = (fn?: (...args: any[]) => void) => { |
| 77 | window[GTAG_FUNCTION_NAME] = (...args: any[]) => { |
| 78 | if (fn) { |
| 79 | fn(...args); |
| 80 | } |
| 81 | // Inject app_name and app_version into events |
| 82 | // TODO(jamesdaniels): I'm doing this as documented but it's still not |
| 83 | // showing up in the console. Investigate. Guessing it's just part of the |
| 84 | // whole GA4 transition mess. |
| 85 | if (args[0] === 'event' && args[2][SEND_TO_KEY] === this.measurementId) { |
| 86 | if (providedAppName) { |
| 87 | args[2][APP_NAME_KEY] = providedAppName; |
| 88 | } |
| 89 | if (providedAppVersion) { |
| 90 | args[2][APP_VERSION_KEY] = providedAppVersion; |
| 91 | } |
| 92 | } |
| 93 | if (debugModeEnabled && typeof console !== 'undefined') { |
| 94 | // eslint-disable-next-line no-console |
| 95 | console.info(...args); |
| 96 | } |
| 97 | /** |
| 98 | * According to the gtag documentation, this function that defines a custom data layer cannot be |
| 99 | * an arrow function because 'arguments' is not an array. It is actually an object that behaves |
| 100 | * like an array and contains more information then just indexes. Transforming this into arrow function |
| 101 | * caused issue #2505 where analytics no longer sent any data. |
| 102 | */ |
| 103 | (function(..._args: any[]) { |
| 104 | window[DATA_LAYER_NAME].push(arguments); |
| 105 | })(...args); |
| 106 | }; |
| 107 | }; |
| 108 | |
| 109 | // Unclear if we still need to but I was running into config/events I passed |
| 110 | // to gtag before ['js' timestamp] weren't getting parsed, so let's make a promise |