When click a text from my website, record into a txt file

Hello, I’ve been investigating and reading on how to capture actions from users in a website. And I just don’t need that much so I made this simple with php:

    $url = $_SERVER['REQUEST_URI'];
    $page = basename($url, ".php");
    $file = fopen("logs/$name.txt", "a") or die("Error");
    $timestamp = date("m-d-Y H:i:s");
    $action = "$timestamp user $useremail has visited $page.\n";
    fwrite($file, $action);
    fclose($file);

So the user loggings to webpage, and log is created or it just appends every page users goes into that log.

Now the thing is I have a section with some links to instructive lessons, and they are word and excels files, so user can read them online or just download them, etc.
I would also like to write on the same log what each user reads or downloads.

I’m guessing I need to use javascript? Or some sort of method to send that click to a small php script like before?

What should I read, investigate more to come up with a solution for what I want to do??

Thanks in advance.

  1. A JavaScript function that fires on a button click event.
  2. Create a Blob constructor, pass the data to be saved in it and specify the type of data.
  3. And finally, call Lifesaver’s saveAs(blob object, “your-file-name.text”) function. js library.

i tell you as much as i know

1 Like

Have you considered using an actual analytics package like Google Analytics or Matomo?

Also, for compliance with GDPR I would recommend against writing email addresses in a log. Use the user ID instead, as that can’t be tracked back to an individual person (if you don’t also have the user database of course).

1 Like

Also, don’t write the log to a file underneath the web root? Maybe?

2 Likes

I hadn’t even realized that :exploding_head: Yeah. That’s a bad idea for sure…

1 Like

I’m going to read about analytics right now, didn’t now you could do that. I’ts my first and only website, so I’m still learning, and it started really small as a simple form and now it looks more like a website. I’ts not that big but even so it hard to keep track sometimes of stuff, and now starting to use github, and I know theres other stuff besides php and html that I should learn to have a more functional website.

Thanks a lot for your advice guys, I’ll change the emails, for ids, and i won’t have them on the root. Maybe ill make a database for them just ids and actions.