How to create connect graphs for Website URL’s?

I’m currently tracking my website activity (url’s visited, google search). I want to construct a connected graph with these URL’s. What is the best approch to do that?

What I have tried:
storing web activites in the below format

{ 
  timestamp: 2024-05-25, 16:13:43, 
  parent_url: https://www.reddit.com, 
  type: visit, 
  url: https://www.reddit.com/t/animals_and_pets/
}.

I’m extracting parent URL using

console.log(document.referrer)

Sometimes the parent URL is empty and other time its incorrect. For example if I have visited site_1 -> site_2 -> site_3. It’s showing parent URL for site_3 as site_1 instead of site_2.

I’m expecting the correct way for getting parent URL’s and structuring my logs.