web - Enable GA and other trackers only for production when assets are prebuilt with webpack -
i'm shipping frontend application want track stats (ga , other trackers). app prebuilt webpack on ci , i'd have same bundle shipped both staging , prod environments. there issue that: if enable trackers during build time they'll enabled on both envs.
one way see add set window.enabletrackers = true
flag in index.html
when sending app code server , decide whether install trackers or not, seems pretty ugly solution.
is there other way u use it? checking domain name on client maybe? like:
if(document.location.hostname === 'prod.example.com') { // enable trackers }
this best resolved @ time compilation made configuring webpack differently production , stage bundles, use cli --define
or --env
explicitly enable tracking.
if don't resolve inclusion of tracking during compilation, have resort other means. 1 option evaluate during runtime whether ga script should loaded in document. here's example of rect element load script if hostname matches configurable regular expression:
import react, {component, proptypes} 'react' const production = new regexp(re_production_host) export const ga = window.ga = window.ga || function () { (ga.q = ga.q || []).push(arguments) } export class analytics extends component { static proptypes = { id: proptypes.string.isrequired }; componentdidmount () { // runtime evaluation loading ga script if (!self.location.hostname.match(production)) { return } const script = document.createelement('script') script.src = 'https://www.google-analytics.com/analytics.js' script.async = true document.body.appendchild(script) } render () { ga.l = +new date() ga('create', this.props.id, 'auto') ga('send', 'pageview') return null } }
here, can still safely define re_production_host
configured production.com
or ^(www.)?production.com%
, can change during test compilations make assertions.
another option add filters in google analytics include/exclude results depending on hostname. won't require make further changes in code.
Comments
Post a Comment