-
Retrieving Google Analytics Data in a Spring Boot Web App
Hey there! So I’ve got this API that has an endpoint
my-api/v1/books/book/{id}
.Pretty straightforward, right? Each “book” is a serialized version available online and they each have a category on my site. The endpoint allows users to see the total views of a specific book by passing the book ID in the URL (like this:
?info=views
).Alright, but here’s where it gets tricky. I’m struggling to figure out how to use Google Analytics with Spring Boot. The tutorials I’ve found are mostly old and don’t cut it anymore. Yes, I know Google has an Analytics Client library for Java but, to be honest, I couldn’t find a decent, up-to-date tutorial on how to use it.
This is how I’m picturing the process:
1 – The user makes the request:my-api/v1/books/book/{id}?info=views
2 – The PathVariable ID is picked up by my BookService.
3 – BookService then looks for the corresponding book. If found, it gets the “slug” like"my-book-01"
or"my-book-01-chapter-02"
. Fun fact: every chapter has its own slug that points back to its book!@Autowired AnalyticsService analyticsService; @Autowired BookRepository bookRepository; // Other methods...// @Override public Long getViews(Long id) { Optional<Book> optionalBook = bookRepository.findById(id); if(optionalBook.isPresent()) { Book foundBook = optionalBook.get(); Long totalViews = analyticsService.receiveDataFromAnalytics(foundBook.getSlug()); // logic to validate the views..... // return totalViews; } //rest of the code...// }
I’ve yet to create the “AnalyticsService” class. But when I do, can anyone tell me how I should structure the request in the
receiveDataFromAnalytics
method?I’d be forever grateful for a good, recent tutorial that could guide me. YouTube is off the table – been there, done that, all outdated.
Log in to reply.