1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to make the best referrer counter

Discussion in 'PHP' started by Robert9, Apr 21, 2019.

  1. #1
    Hi all, i hope to be at the right place here for my question.

    As a result i need something like this:
    referer_short|inc_24|inc_month|inc_all|joindate

    As a temp table i think about something like:
    referer|ip|datetime

    Now i would like to save the first touch and/or the second touch of any visitor in my xenforo xf1 and xf2 also. First touch means the first page, second touch a second page clicked from the first one. short_ref and ref means, that someone could link to me with more than one page from lala.com/page1.html and lala.com/page2.html, but both clicks come from (short) lala.com.

    I am shure there are some needed functions for this in xenforo, anyway i can do it somehow alone. But what is the best way to save data?

    should i update this
    temp_id|referer|ip|datetime|count, if datetime > 60 minutes back from now

    or should i just save
    referer|ip|datetime and use a cronjob every 24hours

    or maybe when the table has {x} lines (also possible by a rand() to empty table every 1/{x} times.

    I have thought about an extern logger, but the ones i have found are so old,
    that they dont have pdo. (like http://rkrt.redkernel-softwares.com/ for example)

    I have thought about heavy guns like pikwik/follower, but not for this reason alone.
    I have thought about fetching data from skynet/google.


    Maybe someone would be so kind and help me out with the best way?

    My goal is as written to know which referrers (urls) have send how many users in the last 24 hours, while i dont want to count a user twice in 60 minutes. And maybe count only these users that have seen at least one more page at my forum. But i dont want to check them all the time? On the other site we have at least two tables for saving "session-data"; so maybe there is anyway what i need or could be with one more field in the table?


    Thank you for advice.
     
    Robert9, Apr 21, 2019 IP
  2. Robert9

    Robert9 Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    I have this one.
    https://github.com/adamdehaven/TrafficTracker
    But it seems to me i need also Google for it?
     
    Robert9, Apr 21, 2019 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    From what I understood,
    You want to track visitors sent from external websites.
    You also want to track only those visitors who also clicked a second page.
    and you do not visitors to be logged twice, within an hour.

    Easiest way, set a cookie as soon as visitor enters your site, if the cookie is not already set.
    Store referer website URL in this cookie.
    cookie expires in an hour

    Now in PHP code:
    $referer= $_SERVER["HTTP_REFERER"];
    $referer= explode( '/', $referer );
    $referer= $referer[2];
    //now only domain name of referer is in variable

    //make sure its not your own domain
    $me= $_SERVER["HTTP_HOST"];
    $me= explode( '/', $me );
    $me= $me[2];

    if( (!isset($_COOKIE["r"])) && $me!=$referer ){
    setcookie("r", $referer, time()+(3600), "/");
    }

    if( isset( $_COOKIE["r"] ) && $_COOKIE["r"]!="" ){
    // log the URL in your database, value is in $_COOKIE["r"]
    //then empty this cookie. Not remove, just empty it
    setcookie( "r", "", time()+(3600), "/" );
    }


    Two things are happening here.
    1. Your code checks if the cookie is set. If not, then it sets one.
    Because cookie cannot be read on the same page where its set, so touch1 gives no cookie, as touch1 itself is setting the cookie.
    so nothing gets logged to database.

    2. Touch2 reads the cookie successfully, logs the referal URL to database, and then sets the same cookie as empty cookie.
    Your code is checking if the value is empty, it will not log anything to database.
    Cookie remains empty for one hour, so nothing happens for the same user for another one hour.

    One hour later, a new cookie is set, with a new referer, if it exists, so same checks happen again.
     
    JEET, Apr 28, 2019 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    If you will depend on database to do your checks, your page load time will suffer a lot, specially if the table grows big.

    This is why use cookies and PHP code to do the checks, and log to database only if something new needs to be stored.
     
    JEET, Apr 28, 2019 IP
  5. Robert9

    Robert9 Greenhorn

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    Yes, this is clear. Thank you.

    But: How to use xenforo´s classes for this? (xf1);
    and how to save it in the best way?

    Example: Some addons counts resources from Resource Manager; the easiest way is to save just the id from the page; then sum it after some time (i have changed this to rand()) and empty the table.
    Should i count like this (insert plus sum plus save in second table plus empty first table) or with an update of one row? Maybe this just doesnt matter?

    Once again my question:
    1) Which classes/methods from xf1 are usable?
    2) How should o store tha data?

    XF1 counts the session of every visitor; so the data is here anyway.
    But i need the first hit and the second one; and also the referrer.
     
    Robert9, Apr 28, 2019 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    I got no idea about XF1 classes package. The code I gave above can be put nto a header php file if any, like in a config file may be which gets included first on any php page.
     
    JEET, Apr 28, 2019 IP