Is there a way to stop Google Analytics counting development work as hits?

后端 未结 22 1769
终归单人心
终归单人心 2020-12-02 04:51

I have added the JavaScript that I need to the bottom of my pages so that I can make use of Google Analytics. Only problem is that I am sure that it is counting all my devel

22条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-02 05:31

    Unfortunatelly, it doesn't seem to be possible to exclude localhost from the reporting when using App + Web Properties type of setup:

    Displaying Filters for web-only Properties only. Filters can't be applied to App + Web Properties.

    For the nextjs web application, especially ones which are using static generation or SSR this would work:

    In your document.tsx

    export default class MyDocument extends Document {
      render() {
        return (
          
            . . . . . 
            
              
    {process.env.NODE_ENV === 'production' ? injectAnalytics() : ''} ); } }

    where injectAnalytics is a function which returns your GA code, for instance:

    function injectAnalytics(): React.ReactFragment {
      return <>
        {/* Global Site Tag (gtag.js) - Google Analytics */}
        
    
                                     
                  
提交回复
热议问题