• 0

[MySQL] Unique visitors


Question

Hi,

I'm writing a statistics script for work. I now have a table that is dynamically filled with every pageview on the site. What I want to do is extract all unique visites from that table. The 2 key things needed are te request time (datetime) and the ip address, I store these in that table.

The problem is that I want to count unique visitors as being a single hit from an IP in a 15 minute period, every other hit from that IP in a 15 minute time period should be discarded. I'm having tremendous problems with finding a solution for this. I've found multiple "solutions" for the problem but none of them work in MySQL or I'm unable to convert them to MySQL.

I hope someone can help me find a solution :)

Link to comment
https://www.neowin.net/forum/topic/663134-mysql-unique-visitors/
Share on other sites

2 answers to this question

Recommended Posts

  • 0
Hi,

I'm writing a statistics script for work. I now have a table that is dynamically filled with every pageview on the site. What I want to do is extract all unique visites from that table. The 2 key things needed are te request time (datetime) and the ip address, I store these in that table.

The problem is that I want to count unique visitors as being a single hit from an IP in a 15 minute period, every other hit from that IP in a 15 minute time period should be discarded. I'm having tremendous problems with finding a solution for this. I've found multiple "solutions" for the problem but none of them work in MySQL or I'm unable to convert them to MySQL.

I hope someone can help me find a solution :)

You'll need a starting reference. have sql log the initial ip & timestamp. use this as your comparison to check as he/she proceeds thru the site by comparing now() to the recorded time. If 15 minutes expired, consider it a new connection and update the starting point.

You'll need to record/check this starting point on every page unless you have a log in required - people can jump into your site at any page unless you're redirecting them (i.e., checking for login credentials).

  • 0
You'll need a starting reference. have sql log the initial ip & timestamp. use this as your comparison to check as he/she proceeds thru the site by comparing now() to the recorded time. If 15 minutes expired, consider it a new connection and update the starting point.

You'll need to record/check this starting point on every page unless you have a log in required - people can jump into your site at any page unless you're redirecting them (i.e., checking for login credentials).

Yeah... what he said. :)

The problem with this is if you want to log EVERY visit to a page(maybe just to record all of the visits for another use) and narrow it down to what youre looking for this way wont do that.

If you added a column to you table as a flag for uniqueness then you can do it. Going back to what Rohdekill said, you can check for a visit within the last 15 mins and if one exists then the unique flag would be 0 and 1 if not. You would need to define the rules for this too...

Uniqueness could be defined as 15mins since the last time the person visited a page, which might mean if the person continues to click around your site for 45mins it would be logged as ONE unique visit. Or it could be defined as 15mins since the last unique visit which would return THREE in a 45 minute span.

The first option wouldnt include a check for the unique flag when you log the visits so..

SELECT T1.is_unique FROM
(SELECT IF(NOW()-MAX(timestamp) >= 15 MINUTES, true, false) AS is_unique FROM visitors WHERE visitor_ip = '$ip') T1
WHERE T1.visitor_ip = '$ip'

Should return true or false

The second option would be something like

SELECT T1.is_unique FROM
(SELECT IF(NOW()-MAX(timestamp) >= 15 MINUTES, true, false) AS is_unique FROM visitors WHERE visitor_ip = '$ip' AND unique_flag = 'true') T1

I forget which function will determine the 15 MINUTES, its something like INTERVAL() I think. You could probably even wrap the insert statement around this query somehow to do most of the work in SQL rather than running 2 seperate queries.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Qualcomm's new Snapdragon Reality Elite chip brings on-device AI to Android XR devices by Pradeep Viswanathan Qualcomm has been delivering dedicated SoCs for mixed reality and spatial computing devices for several years. The journey started with the Snapdragon XR1, followed by the Snapdragon XR2 in 2019, the Snapdragon XR2 Gen 2 in September 2023, and finally the Snapdragon XR2+ Gen 2 in 2024. Today, Qualcomm announced a major upgrade with the new Snapdragon Reality Elite Platform, which targets premium mixed reality and spatial computing devices. OEMs can use this SoC to power both all-in-one video-see-through headsets and lightweight, tethered optical-see-through glasses. Qualcomm highlighted that the Snapdragon Reality Elite will power the next wave of Android XR devices coming later this year. These wearables will offer better visuals, improved power efficiency, and deeper on-device AI integration compared to the previous generation. The Snapdragon Reality Elite can deliver up to 48 TOPS of AI performance, allowing large language models and large vision models to run directly on the device for the first time. In addition to enabling new spatial AI experiences, these new AI capabilities will improve head and hand tracking, as well as see-through features. On the performance side, the Snapdragon Reality Elite offers up to 60% higher GPU performance, up to 30% higher CPU performance, and up to 160% higher NPU performance compared to the previous generation. The platform supports visuals of up to 4.4K per eye at 90 frames per second for sharper images and smoother motion. Qualcomm is also claiming significant efficiency improvements. The Snapdragon Reality Elite can offer up to 20% longer battery life under the same workload. More importantly, the chipset can run up to 12 degrees Celsius cooler under load, making headsets more comfortable for users to wear for longer periods. The platform also includes improvements to video see-through, featuring lower latency and better image quality. Qualcomm states that its EVA hardware block helps accelerate demanding computer vision workloads, improving how digital content blends with the real world.
    • Umm... GitHub continues to use AWS. That's the story, that's the headline. There's no "new" news here. GitHub continues to require additional capacity beyond the originally-planned Azure allocations. There's nothing special about this; nothing noteworthy. They're still using AWS' infra until the cutover is complete.
    • Hello, Also known for https://www.theguardian.com/technology/2009/jan/29/adware-internet.   Regards, Aryeh Goretsky    
    • Hello, I have used a few TEAM Group SSDs, USB flash drives, and Micro SDXC cards in the past. They all seemed to work fine. Regards, Aryeh Goretsky
    • "just $100 per TB"? Just? Are we trying to make this seem like the new normal? Kinda weird to make it sound like that is not a ridiculously expensive asking price.
  • Recent Achievements

    • Collaborator
      vjlex earned a badge
      Collaborator
    • Reacting Well
      Dys Topia earned a badge
      Reacting Well
    • Conversation Starter
      NovaEdgeX earned a badge
      Conversation Starter
    • One Year In
      Console General earned a badge
      One Year In
    • Week One Done
      Twozo Technologies earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      517
    2. 2
      +Edouard
      182
    3. 3
      PsYcHoKiLLa
      106
    4. 4
      Steven P.
      88
    5. 5
      ATLien_0
      68
  • Tell a friend

    Love Neowin? Tell a friend!