Search code examples
google-analyticsgtag.js

fire gtag event from a local machine / different domain


im trying to fire a gtag event (analytics) from a local machine in windows. is it possible ?

i tried to change my domain in the hosts file to be like my analytics domain , but it didnt work. events from prod env worked fine. the only differeces between the calls were:

1.the local website sent an http domain dl=http:// /login?returnUrl=/& ,

  1. gtm - which is probably time and supposed to be different .

  2. _gid - which sounds like id of call , which also sounds like should be different.

we fire a call using gtag mnethod:

gtag('test', 'test6666', { 'event_category': 'test', 'event_label': 'test6666' });
https://www.google-analytics.com/collect?
v=1&_v=j98&a=1506331396&t=pageview&_s=4&
dl=<domain>/login?returnUrl=/&
MessCode=4&ul=en-us&de=UTF-8&
dt=<dt>&sd=24-bit&sr=1680x1050&vp=1680x897&je=0&
_u=SACAAUABAAAAACAAI~&jid=&gjid=&cid=1729439813.1669892239&
tid=UA-960228-11&_gid=1536981198.1669892239&gtm=2oubu0&z=865507838



https://www.google-analytics.com/collect?
v=1&_v=j98&a=1995681705&t=event&_s=3
&dl=<domain>/login?returnUrl=/&
MessCode=4&ul=en-us&de=UTF-8&
dt=<dt>&sd=24-bit&sr=1680x1050&vp=1084x907&je=0&
ec=Login&ea=ClickAd&el=DbshopLookforBusinessb2c&
_u=SACAAUABAAAAAAAAI~&jid=&gjid=&cid=912047780.1669116634&
tid=UA-960228-11&_gid=1394515120.1669799137&gtm=2oubs0&z=612472842

Solution

  • It's most definitely possible to call gtag locally.

    First, make sure your dev env deploys gtag.js.

    Next. make sure that gtag is a funciton declared as a dataLayer push.

    Finally, make sure dataLayer is defined.

    Now to see your event, gtag('configure') has to run first for the lib to know where to send your event, so make sure configuration has been run. Also consider not using your prod measurement id for testing in lowers.

    Finally, using gtag like that is not the best practice. It's a hack. Proper implementation should go through GTM. It will allow much better scalability, maintainability, ease of use and what not.