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

后端 未结 22 1662
终归单人心
终归单人心 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:29

    Today, whilst on a different computer than my own, I noticed μBlock Origin for Chrome was blocking Google AdSense by default. After some Googling, I found this article. It notes also μBlock Origin Firefox, μ Adblock for Firefox and Ad Muncher for Windows block AdSense by default. Most other options are listed as being configurable to block AdSense.

    This seems to work and is useful because my IP is often dynamic, so the Chrome extension can follow me around as long as I am logged in to Chrome.

    0 讨论(0)
  • 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 (
          <Html lang="en">
            . . . . . 
            <body>
              <Main />
              <NextScript />
              {process.env.NODE_ENV === 'production' ? injectAnalytics() : ''}
            </body>
          </Html>
        );
      }
    }
    

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

    function injectAnalytics(): React.ReactFragment {
      return <>
        {/* Global Site Tag (gtag.js) - Google Analytics */}
        <script
          async
          src={`https://www.googletagmanager.com/gtag/js?id=${GA_TRACKING_ID}`}
        />
        <script
          dangerouslySetInnerHTML={{
            __html: `
                        window.dataLayer = window.dataLayer || [];
                        function gtag(){dataLayer.push(arguments);}
                        window.gtag = gtag;
                        gtag('js', new Date());
    
                        gtag('config', '${GA_TRACKING_ID}', {
                          page_path: window.location.pathname,
                        });
                      `,
          }}
        />
      </>
    }
    
    0 讨论(0)
  • 2020-12-02 05:33

    It's 2014 and I'm still unsatisfied with all existing solutions...

    • IP filters require a static IP address. What if I'm working from home or from a coffee shop?
    • Checking host name eliminates hits from a dev environment, but what if I'm debugging the live site?
    • Editing server configurations is annoying/advanced and multiple domains are complicated.
    • Opt-Out extensions either block hits on all websites or none at all depending on who you ask.

    So, I created my own Browser Extension... https://chrome.google.com/webstore/detail/lknhpplgahpbindnnocglcjonpahfikn

    • It follows me wherever I go
    • It works on a dev environment and on live/public domains
    • It only affects me and the sites that I'm developing
    • It turns on/off with one click
    • It's easy to verify that it is truly not sending any data to analytics

    It works by keeping a "developer cookie" set on your machine at all times just for the domains that you choose. You then simply check for this cookie in your script before sending any data to Analytics. You customize your own unique NAME and VALUE for the cookies in the extension's settings. This can easily be used by a team of people, so developers, content creators, proofreaders, and anyone else in your organization can all view pages without inflating the statistics.

    Examples of how to put the code into your pages...

    JavaScript

    if (window.location.host==="mydomain.com" || window.location.host==="www.mydomain.com") {
       if (document.cookie.indexOf("COOKIENAME=COOKIEVALUE") === -1) {
          // Insert Analytics Code Here
       }
    }
    

    PHP

    if ($_SERVER['HTTP_HOST']==="mydomain.com" || $_SERVER['HTTP_HOST']==="www.mydomain.com") {
       if (@$_COOKIE["COOKIENAME"] !== "COOKIEVALUE") {
          // Insert Analytics Code Here
       }
    }
    

    Verifying that the HOST name equals the domain of your live site ("mydomain.com") ensures that the analytics data will never be sent by ANY visitor while viewing from a test domain such as "localhost" or "beta.mydomain.com". In the examples above, "www.mydomain.com" and "mydomain.com" are the two valid domains where we DO want visits to be recorded.

    The live site sends data to analytics as expected UNLESS a developer cookie is found with matching values. If it sees that unique cookie set on your device, then your visit will not count towards your totals in Google Analytics or whatever other analytics tool you prefer to use.

    Feel free to share my solution and use my extension to keep those cookies set.

    0 讨论(0)
  • 2020-12-02 05:33

    To disable localhost hits, just create a filter to exclude localhost. Go to Admin -> Property -> View Settings to do so. Check the following screenshot for some help.

    To disable production URL hits for yourself if you visit using a non-static IP, you can use a Chrome extension like Developer Cookie to skip running the Google Analytics code if it's you.

    I personally don't do this since I use an Ad Blocker which already blocks Google Analytics on my browser.

    0 讨论(0)
  • 2020-12-02 05:34

    The solution is to use Google Tag Manager (GTM) to handle your Google Analytics. This will allow you to only fire Google Analytics on your production domain without having to write any conditionals in your site's code. Here's how to do it:

    In GTM, set a Trigger that only fires when the Page Hostname contains your production domain.

    Then set a Tag for Universal Analytics and make its Trigger the one you just created.

    0 讨论(0)
  • 2020-12-02 05:35

    There are a few Chrome extensions that do this for you, like https://chrome.google.com/webstore/detail/fadgflmigmogfionelcpalhohefbnehm

    Very convenient if your IP address is not static.

    0 讨论(0)
提交回复
热议问题