简体   繁体   中英

Google Analytics without JavaScript or Cookies

I have a unique situation where I have a site with several thousand users who are all browing using a client that does not support JavaScript or cookies.

It is also unable to load normal image files, but instead will only load a special image type, NTFT

<img src="http://example.com/image.ntft" height="1" width="1" />

The client isn't a web browser per se, but renders a non-standard form of HTML.

I want to use Google Analytics to get visitor information for my site, and get unique visitor information. I get several hundred thousand hits and about ~40GB of bandwidth usage a day, so I'm very interested in the information for unique visitors. I've been looking at https://github.com/thomasbachem/php-ga but do not want anything like this: How do I get PHP-GA to stop incrementing real-time visitors in google analytics? to happen, as I'll get swamped with "Unique Visitors" that are really just one page visit.

I'm using PHP. Can you suggest a decent solution to measure traffic from unique visitors (unique IPs, for example)?

I would consider giving PIWIK a try. You can assign your users a unique ID and track them via the tracking API

http://piwik.org/

Tracking API DOCS: http://piwik.org/docs/tracking-api/

PHP Client for Tracking API http://developer.piwik.org/api-reference/PHP-Piwik-Tracker

Another option would be to implement the Google Measurement Protocol. Since you can define your own definition of a unique visitor by passing the cid parameter. For example a hashed ip-address or another unique ID based on the user. Please not that it is not allowed to pass the plain ip number (or any user data that can identify a user) so always use hashed values.

By using the GA measurement protocol you can use all the features provided by GA without the need of javascript.

PHP client: https://github.com/krizon/php-ga-measurement-protocol API Doc: https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide

disclaimer: i wrote the php client.

Here an useful code to agree with the regulation of cookies in Europe. It works at this moment (november 2020) :

<script>
const cyrb53 = function(str, seed = 0) {
   let h1 = 0xdeadbeef ^ seed,
      h2 = 0x41c6ce57 ^ seed;
   for (let i = 0, ch; i < str.length; i++) {
      ch = str.charCodeAt(i);
      h1 = Math.imul(h1 ^ ch, 2654435761);
      h2 = Math.imul(h2 ^ ch, 1597334677);
   }
   h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
   h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
   return 4294967296 * (2097151 & h2) + (h1 >>> 0);
};

let clientIP = "{Client_IP}";
let validityInterval = Math.round (new Date() / 1000 / 3600 / 24 / 4);
let clientIDSource = clientIP + ";" + window.location.host + ";" + navigator.userAgent + ";" + navigator.language + ";" + validityInterval;
let clientIDHashed = cyrb53(clientIDSource).toString(16);

(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'Your-analytics-code', {
   'storage': 'none',
   'clientId': clientIDHashed
});
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>

You have to replace Your-analytics-code. In php you can get the ip of client "{Client_IP}" by this way:

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
    $ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM