this script allows for multiple google analytics accounts to be set up and controlled as a package. so if a custom event is triggered, it will send to all ua numbers that the analytics object has been configured with. it simplifies demographics, custom page views, and custom events and has some test/debug options to see the data that would be sent to ga without actually sending.
<script type="text/javascript" charset="utf-8" src="/path/to/universal_analytics.js"></script>
configure ua accounts in a diff script/in the html body
- test/debug ua
_analytics.ua.test = { global:'UA-0000-3' };
- live ua
_analytics.init({ global:'UA-0000-1', subdomain:'UA-0000-2' });
try { _analytics.init({ primary:'UA-5854796-6' }); } catch(error){ console.log(error); }
window.onload = function(){ try { if(typeof _anchor!=='undefined') _anchor.init(); } catch(error){ console.log('_anchor error'); console.log(error); }; }
- page_view: send ga page view on init [boolean]
- display_features: enable ga demographics [boolean]
_analytics.init(
{ global:'UA-0000-1', subdomain:'UA-0000-2' },
{ page_view:true, display_features:false }
);
display what would be sent to ga in the console without sending data to your actual account(s), disabed by default
- javascript:
_analytics.ua.debug = true;
- append ?debug / &debug or #debug to end of the url
can be called at any time, example uses for calling after main page view would be for things like tracking pushstate / ajax content switches
- pv_page
- description: page path
- type: string
- optional
- default: current path
- pv_title
- description: page title
- type: string
- optional
- default: current title
_analytics.page_view('/page_name','Some Page Title');
track a custom event
- category
- description: typically the object that was interacted with (e.g. button)
- type: string
- required
- action
- description: type of interaction (e.g. click)
- type: string
- required
- label
- description: useful for categorizing events (e.g. nav buttons)
- type: string
- optional
- value
- description: values must be non-negative, useful to pass counts
- type: integer
- optional
$(document).on('click','.button',function(){
_analytics.page_event('global button','click',$(this).text());
});