Search code examples
angulargoogle-analytics

Tracking visitors to site where there's no login with google analytics


I built a documentation site for an open source CSS tool I created. I built the site using Angular 16 and used the ngx-google-analytics package to add google analytics to keep track of page views and how people are interacting with the demonstrations I created on the site. So far I've been able to use the page_views and events functions that come with the service file in this package to successfully report activity to my analytics account. However I want to track individual users as well which in the package documentation can be done by passing an object into the function like this

this.$gaService.pageView(currentRoute, `Visited ${currentRoute}`, undefined, {
  user_id: 'their_user_id'
});

Seeing that this is a documentation site like virtually every other website for the frameworks and languages we use, there is no need for them to sign up and login to any account. I saw when studying google analytics that we can default to using their google id so long as they're logged in to their google account but I haven't seen anything showing how to do that. However I'd rather not do that because there's no guarantee that everybody will be logged into their google account. I just want to be able to distinguish all the activity from one user to another because as it stands right now I'll just get a bunch of events with no way of knowing whose doing what to in turn hone in on one user's session. Do I need to use their IP address? Does GA do something under the hood that handles this for us that I need to activate? How can I do this?


Solution

  • Yes, GA does something under the hood to distinguish one user from another. No need to activate. You only need to set it if you're trying to run it on the backend rather than front-end. But I don't see why you'd want to do that.

    Go test your package. You will notice cookies from Google starting with _ga. That's where it stores the unique client id. Don't stop there. Check the Network in devtools, filter by collect, check the pageload. See the cid param? That's how it sends it to its endpoint.

    Make sure you're using GA4 rather than GA UA. Looks like that package was updated a year ago. I didn't look deep enough to see if it supports GA4.