Detect when visitor entering/leaving my website

Hi everyone,

Is it possible to detect when a visitor/customer just entered my website or leave it (like if a new session is started or closed between the client and the website).

Can it be done in the liquid files? Is there another way that I can detect that?

Eventually I want to send the information to Google Tag Manager with dataLayer.push()

Thanks