We are a vendor whose integration sends custom events to our clients' previously self-installed GA(s).
For pre-GA4, window.ga.getAll().forEach( t =>...
iterates through all current "trackers", enables getting their UA-XXX
IDs via t.get('trackingId')
, and enables sending of events to each via t.send( 'event'...
.
For GA4, in the case it happened to have been installed via "the gtag method", gtag( 'event'...
seems to successfully send events to all current G-XXX
"measurement ID(s)" aka "tag ID(s)", as desired.
However for GA4 in the case it happened to have been installed via "the GTM method", incrementally "installing" gtag() seems to enable us to successfully send events with gtag( 'event'...
, but only when explicitly including 'send_to' : 'G-XXX'
. Without send_to
, no collect
endpoint is generated in Network
tab. We have confirmed this with a fresh GA4 install via GTM, and then full gtag installation snippet, on a dummy site where we are testing this. We have tried various iterations of methods to get it to work and have not yet found one.
So for this case of GTM-installed GA4, we're looking for either (a) confirmation of some method to enable using gtag( 'event'...
without send_to
; or alternatively (b) a programmatic way to get all G-XXX
ID(s) analogous to our pre-GA4 method above, in order to explicitly use them in gtag( 'event'...
's send_to
; or (c) some other way of programmatically sending GA4 events other than gtag( 'event'...
.
For scale reasons, we cannot have each client configure each custom event in GTM (and later make changes according to releases), and we do not have access to our clients' GTM to do that ourselves. Noting however that we do have access to our clients' GA.
solution by (b) method above (return array of all GA4 IDs, if any):
Object.keys( window.google_tag_manager || [] ).filter( e => { return e.substring(0,2) == 'G-' } )
i've tested this across a couple dozen client sites and it currently works as intended across various situations including no-GA4 and (despite the name) both the cases of GA4 installed by GTM method, or by gtag-only method.
noting the direct access of window.google_tag_manager
may not be officially supported so it's possible programmatic dependence on it could be brittle. that said, so far this is the best method i've found for our purposes. noting other potential methods, depending on needs, could be retrieving IDs from cookie, or by extracting them from other collect
endpoint calls.