Joshua's Docs - Google Analytics

Google Analytics

Resources

Versioning

Web Library Docs Primary Globals Extra Globals
Legacy (ga.js) Docs _gat (tracker methods)
_gaq (async push methods)
_gaq.push()
gaGlobal
Analytics.js Docs ga()
ga('send',{})
gaGlobal
ga
GoogleAnalyticsObject
google_tag_data
gaplugins
gaData
Gtag.js Docs gtag()
gtag('event','{{action}}',{})
gaGlobal
ga
GoogleAnalyticsObject
google_tag_data
gaplugins
gaData

Plus
google_tag_manager
gtag
dataLayer

Example setup tags

Legacy (ga.js)

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-12345678-9']);
  _gaq.push(['_trackPageview']);
  (function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

Analytics.js

<!-- Google Analytics -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  
  ga('create', 'UA-12345678-9', 'auto');
  ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

gtag.js

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-12345678-9"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-12345678-9');
</script>

Manual Tracking

Legacy (ga.js)

Pageview:

_gaq.push(['_trackPageview']);

Event:

// Recommended way is to "push" into _gaq global
_gaq.push(['_trackEvent', category, action, opt_label, opt_value, opt_noninteraction]);

// Or use tracker "instance", if sending to specific tracker
_gat._getTrackerByName('myTracker')._trackEvent(category, action, opt_label, opt_value, opt_noninteraction);

Analytics.js

Pageview:

ga('send', 'pageview', [page], [fieldsObject]);

Event:

ga('send', 'event', eventCategory, eventAction, eventLabel, eventValue);

// Or, with args as obj
ga('send', 'event', fieldsObject)

gtag.js

Pageview:

// Unless you config command with send_page_view as false, the intial config will fire a pageview automatically:
gtag('config', 'GA_MEASUREMENT_ID');

// Or, you can fire programmatically:
gtag('config', 'GA_MEASUREMENT_ID', {<pageview_parameters>});

Events:

gtag('event', action, {
  'event_category': category,
  'event_label': label,
  'value': value
});

Gotchas

  • Events are limited to 20 immediately, then 2/second rate-limited, then 500 total per session

Filters

  • If you want to filter by domain of referral source (not just path part (e.g. /subpage)), you'll want to use Filter Field "Campaign Source"
Markdown Source Last Updated:
Thu Feb 27 2020 06:30:23 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Mon Aug 19 2019 17:06:24 GMT+0000 (Coordinated Universal Time)
© 2024 Joshua Tzucker, Built with Gatsby
Feedback