How to test % of users with javascript turned on
Trying to test the # of users if javascript is turned on. I thought this trick would work, but forgot that the php executes either way.
<script>
<?php
$sql_insert = "INSERT IGNORE into `test_javascript` (`javascript`,`dateTime`) VALUES ('1',now())";
mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
?>
</script>
<noscript>
<?php
$sql_insert = "INSERT IGNORE into `test_javascript` (`javascript`,`dateTime`) VALUES ('0',now())";
mysql_query($sql_insert,$link) or die("Insertion Failed:" . mysql_error());
?>
</noscript>
Any ideas on how to fix this script or other way开发者_Python百科s to find % of users with javascript turned on.
60% of our users use IE, and a few of them (that we know of), don't have javascript enabled, so I'd like to get a better sense of the number of users without javascript.
You could use AJAX, get javascript to call a separate page which will process your database query with PHP
When your page loads, you can insert an entry into a database in PHP. This will give you the total number of page requests.
In Javascript, you can initiate another request using AJAX. This would post to a PHP script, but requires Javascript to work properly. This query would tell you the number of requests of browsers with Javascript support.
If you log timestamps and IP addresses (or any other state variables), you should be able to correlate the entries between the two insertions.
Or, another method would be to have the AJAX call update the row inserted by the PHP query. This would be a cleaner approach in my opinion.
You could do ajax, as others suggest or you can just log everyone and insert script that adds something like
document.write('<img src="/javascript-enabled-tracking" width="0" height="0"/>');
pointing to your php script that counts javascript-enabled folks.
Moreover, you probably can put image into noscript
section instead, in which case you need no javascript for it and it will track non-js-enabled souls.
Log everybody to count how many people visit your website (no script or noscript tag) and then log javascript enabled people using an AJAX call.
Both blocks of PHP will run as the server-side code is executed before the client-side, therefore your script
/noscript
is irrelevant.
What you can do is write a cookie via PHP on first page load, then use JS to set another cookie. On the next page load you can check to see if both cookies have been set, if only the PHP one is set then you can assume that JS is disabled. Of course if the user has cookies disabled then you won't know!
Alternatively you could use script
/noscript
to load a tracking pixel image.
<script>
trackingPixel = new Image();
trackingPixel.src = '/tracking.php?yesjs';
</script>
<noscript>
<img src="/tracking.php?nojs" alt="">
</noscript>
Then have your tracking.php file record the information as needed and to disable the check in future requests. Make sure you return something appropriate at the end of the script (MIME type, content, etc).
You could use js and AJAX to call a PHP page which includes the insert code. Thus, the call will only be made if the person has javascript enabled.
You'll probably want to make sure that you pass something into this PHP page in the AJAX POST so that anyone who might happen to land on this page (like a bot or something) doesn't trigger a submit to the DB.
Generate a unique ID for each user accessing your server (SessionID for example). Inject (if not allready counted) a javascript sending an ajax request to your counter using this ID.
The simplest solution is probably injecting this into your pages:
<script>document.cookie = "javascript=1";</script>
And then checking $_COOKIE["javascript"]
. But of course this only works on the second page/request. (More elaborate probing methods thus use a redirect to a detecting page, if no cookies are yet present.)
Here's an outline of a strategy:
- First, you would need a suitably reliable way of knowing when a 'user' viewed your page
- Second, you would need to put that id into a cookie so once they have the cookie, you can test for it; store this in a db table
- Now, on page load and if the cookie is available, try setting another cookie with JS; salt it with the user ID
- On page request (or maybe with AJAX), test for the presence of the id and test cookie; store this in a table where the user id and test cookie field are both unique only
- Do a query to see how many id's do not have corresponding test cookie values
You should be able to assign page requests an ID, log this into the database as an HTML request, and then add something like
<script type="text/javascript" src="/tracker.php?id=<?php echo $ID; ?>"></script>
which then logs JS requests to the database. Users with JavaScript disabled won't request the file.
精彩评论