Analysis of buried point technology and simple implementation with JS+PHP (with code)

PHP technology exchange QQ group: 490910618
I What does that mean?

Buried point: vernacular is to bury a code on the page, and actively submit a series of data such as relevant information of this page to the server through this code.

Function: used to collect website data for data analysis. (there are a lot of data collection, such as which ip, which page, and so on)

II How to bury it?

There are two ways of embedding points:

                1. Those using Daniel platform (Baidu / Google / Youmeng / Tencent) have very mature data acquisition and analysis platforms with powerful functions. It's one thing to spend money.

                2. If you write this yourself, you'll have plenty of food and clothing. I wrote it. The amount of code is not very large. It's very simple. If the website doesn't have very high requirements for analysis functions, you can write it yourself. If you want to write by hand, read on. If you want to use the ready-made ones, you can consult the customer service of the relevant platforms. The customer service consultation and after-sales service are very good. After all, in addition to selling products, they also sell services. After all, there are many platforms. Why can some platforms excel? Right? In the final analysis, the service is good.

III Implementation principle of buried point

The user clicks the page - burying point requests to load a js file (the most important thing of the js file is a pseudo image. src requests the php file of the server with parameters. The code in the php file is to analyze or store the collected data. Here, my husband becomes a log file and then stores it in the database in batches) The purpose of using pseudo files here is to solve cross domain problems. It's not detailed here. Check a lot on the Internet.

IV code implementation

First, add a js code to the page where you want to collect data and add it between < \ body > and < \ HTML >:

<script type="text/javascript">
var _maq = _maq || [];
_maq.push(['user_id', '89798']);
(function() {
    var ma = document.createElement('script'); 
    ma.type = 'text/javascript'; 
    ma.async = true;
    ma.src = './report.js';
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(ma, s);

Then, here we request a report js file. Just put this js file in the root directory of the website, such as WWW / test / report js, js file write code:

(function () {
    var params = {};
    if(_maq) {
        for(var i in _maq) {
            switch(_maq[i][0]) {
                case 'user_id':
                    params.user_id = _maq[i][1];

    var args = '';
    for(var i in params) {
        if(args != '') {
            args += '&';
        args += i + '=' + encodeURIComponent(params[i]);
    var img = new Image(1, 1);
    img.src = './report.php?' + args;
Then, here we request a php file, which is put in the report JS path, and write the following code to the php file (the comment part is to obtain the browser information, etc. I only want to pass the id myself, so I comment it out here, and open the comment if I want. Here, for the convenience of writing the log to the mysql database later, everything is simplified, leaving only the necessary fields):
// logger('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].(empty($_SERVER['QUERY_STRING'])?"":("?".$_SERVER['QUERY_STRING'])));
// logger($postStr);

foreach ($_GET as $key => $value) {

// foreach ($_SERVER as $key => $value) {
//     logger("_SERVER: Key: $key; Value: $value");
// }

function logger($log_content) {
    $max_size = 10000;
    list($time0, $time1) = explode(' ', microtime());
    $time2 = date('Y-m-d H:i:s', $time1).":".round($time0 * 1000);
    $log_filename = $_SERVER['DOCUMENT_ROOT'].'\\'.date('Ymd', time()).'.log';

    if(!is_file($log_filename)) {
        $fopen = fopen($log_filename, 'w');

    if(file_exists($log_filename) && (abs(filesize($log_filename)) < $max_size)) {
        // unlink($log_filename);
        // file_put_contents($log_filename, $time2." ".$log_content.PHP_EOL, FILE_APPEND);
        file_put_contents($log_filename, $log_content.PHP_EOL, FILE_APPEND);

After the user clicks the page, the code performs these operations, and then generates the log file in this format I want:

Normal format: (format before simplification)

Simplified format: user_id, timestamp (convenient for batch storage in database)

Here, use the buried point to collect website data. If you are interested, try it. It's simple.

After that, I will send another article to explain the batch storage of mysql and the activity of counting days, weeks and months.


Added by mightymouse on Sat, 08 Jan 2022 19:31:52 +0200