Wanna know where they've been?

I mean: your visitors. Would you like to know where they've been?

Well, you can know.

But don't get me wrong here. You can't know about their entire browsing histories. That would violate their privacy.

However, if a visitor clicked on a link from another website to get to your website, you can gather that info.

In this example, I'll explain how to do that if you're using a Spring Boot application to handle HTTP requests to your website.

If you're using PHP or some other tech stack, you'll have to keep searching.

Service Requested

As an example, I'll share with you some of the code that powers this very website.

Yep, this site is managed by a Spring Boot app that runs in a Docker container. The whole thing sits behind an Apache web server that, not coincidentally, also runs in a Docker container.

That was a pain to get configured.

Anyhoo, here's the method that got executed when you visited this web page:

    public String readPost(@PathVariable String slug, Model model, HttpServletRequest request, 
	        HttpServletResponse response, Device device) {
	Optional<BlogPost> post = blogPostRepository.findBySlug(slug);
	if (post.isPresent()) {		    
	    LOG.info("User viewing slug " + slug);
	    BlogPost blogPost = post.get();

	    analyticsUtil.savePageVisit(blogPost, request);

Well, that's part of the method, anyway. Note the ellipsis.

The key thing to focus in on here is the third parameter in the readPost() method. It's an HttpServletRequest object.

I use that method to determine where you were before you got here. Chances are good that you found this page via the Big G.

And I need to know that because I need to know how I'm getting my traffic.

(Sidebar: I don't use Google Analytics because it slows down the site and then, ironically, Google lowers my website's rank in search.)

By the way: you can include that HttpServletRequest object in any controller method that handles requests. It's an object that's nice to have when you need this kind of info.

But back to the point of this section. I mentioned that I need to gather website analytics.

That's why that analyticsUtil.savePageVisit() line exists. It logs info about visits, such as the time you visited, the page visited, where you came from, and more.

And note that savePageVisit() requires an HttpServletRequest object as its second parameter.

So let's see how that method works.

The Analytics Utility Belt

The code that logged your visit looks like this:

    public void savePageVisit(BlogPost blogPost, HttpServletRequest request) {
        WebPageVisit visit = new WebPageVisit();

        String referer = request.getHeader("referer");


Hone in on the "referer" line.

That's where the code grabs the info about the website that sent you here.

So that's what you'll need to do if you want get that info as well. It works like a charm.

Now keep in mind, when you use that, you'll get back the entire URL of the website that sent the visitor to your page.

So in your case, the application probably just logged "https://www.google.com".

Cuz that's probably how you got here.

So if you're looking for just the brand name of the referring page (i.e., "Google"), you'll have to do some parsing and translating.

Ya Got Took?

Also keep in mind: that header is controlled by the client. That means it can get spoofed.

In other words, you could get garbage info this way. But only from malicious jerkweeds.

Otherwise, you should get good data.

The point is: don't use it for anything critical.

Since I'm just using it for analytics, I can live with the occasional crap data. It won't affect the overall numbers significantly.

Wrapping It Up

Now you know how to get the URL of the page that sent a visitor to your website.

That is, if your website is powered by Spring Boot.

Feel free to take what you've learned here and apply it in your own code.

Have fun!

Photo by Sean Patrick from Pexels