1

I want to give website visitors a chance to opt out of Google Analytics. However, the website I am working on is in PHP, so Google's Analytics seems to happen all on the server side. Visiting the webpage "inCognito, inPrivate or with private browsing" does not stop Google from accessing the general location, etc.

Is there a simple way while still keeping the page PHP rather than HTML to give the user a chance to opt-out?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
Jonathan Abbott
  • 180
  • 1
  • 10

1 Answers1

1

Here's a simple example you can build on:

<html>
    <head>
        <?php
            if(!$blockGA)
                echo '<script type="text/javascript">

                    var _gaq = _gaq || [];';
                    // etc
        ?>
    </head>
    <body>
        Hello world!
        <?php
            // Some more PHP
        ?>
    </body>
</html>

You could store the setting in a database, store it in a SESSION, or simply use a persistent query string like $_GET['blockGA'].

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
  • 1
    Or you could set a long period (1 year) cookie – machineaddict Apr 30 '13 at 07:16
  • 1
    @Danny Thanks. I don't want to ever explicitly ask users, so I might simply check if [cookies are enabled](http://stackoverflow.com/questions/531393/how-to-detect-if-cookies-are-disabled-is-it-possible) and then use the above. – Jonathan Abbott Apr 30 '13 at 10:23
  • @JonathanAbbott Just in case you didn't already know, you can get and set cookies directly with PHP; no JS is necessary: http://php.net/manual/en/function.setcookie.php – Danny Beckett Apr 30 '13 at 10:24
  • Ended up not using cookies as inCognito, inPrivate or with private browsing seem to allow cookies during the session. Best to point users towards [Google Analytics Opt-out Add-on](https://tools.google.com/dlpage/gaoptout) or work with [DNT](http://en.wikipedia.org/wiki/Do_Not_Track) (Do Not Track) – Jonathan Abbott Apr 30 '13 at 15:12