Learn business growth with Google Analytics 4 Forums Google Analytics 4 Retrieving Google Analytics Data in a Spring Boot Web App

  • Retrieving Google Analytics Data in a Spring Boot Web App

    Posted by Xavier on 20 October 2022 at 7:46 pm

    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.

    Henry replied 1 year, 4 months ago 3 Members · 2 Replies
  • 2 Replies
  • Connor

    Member
    4 January 2023 at 3:04 pm

    In this case, the user is asking for assistance in creating a specific service class in his Spring Boot application, which will allow for interaction with Google Analytics. They need to develop the “AnalyticsService” class, specifically the “receiveDataFromAnalytics” method, to request data from Google Analytics based on a given string parameter (or “slug”). This would help them retrieve the total views for a specific online book in their application, using Google Analytics with Spring Boot. The user is also looking for up-to-date tutorials or resources, aside from YouTube, that can guide them in implementing this with the latest Google Analytics Client Library for Java.

  • Henry

    Member
    23 April 2023 at 10:37 am

    Let’s break down the task we need to accomplish. The “AnalyticsService” will be receiving the slug of a book and it needs to communicate with Google Analytics to get the view count. For this, you will have to use the Google Analytics Reporting API v4.

    When you start creating your AnalyticsService, you will need to first initialize a service object with your credentials which you can store as environment variables. These credentials would be your API key, client id and client secret that you can get from the Google Cloud Console. You will need to ensure you have the reporting API enabled for your project.

    After initializing your service object, you would then be calling the ‘batchGet’ function on the service object inside your “receiveDataFromAnalytics” method. You would input a ‘GetReportsRequest’ object where you would set the ‘viewId’, ‘dateRanges’, ‘metrics’, and ‘dimensions’ in a ‘ReportRequest’ object and put that inside a list. The ‘viewId’ would be your Google Analytics view ID, ‘dateRanges’ would determine the range of dates you want the data from (you can set it to ‘allTime’ if you want the total views), and ‘metrics’ and ‘dimensions’ would depend on what you are trying to get. In your case, for ‘metrics’ you would set the ‘expression’ as ‘ga:pageviews’ and in ‘dimensions’, you would set the ‘name’ as ‘ga:pagePath’. This basically means that you are asking for a report of the page views per page path.

    Next, you would read the response. This would be in the form of a list of report objects and each object would contain columnHeader and data properties. You can thus loop through the list and get your measurements from the ‘metrics’ array in the ‘rows’ array in the ‘data’ property.

    Remember to catch any possible exceptions that may arise due to failure of the API call or in case the slug doesn’t exist in your Google Analytics data. It’s always good to have substantial logging in place so you can understand where things may have gone wrong if they do.

    Unfortunately, I don’t have a tutorial to recommend, but I suggest you keep an eye on the Google Analytics API official developers documentation as Google keeps it updated. I understand that it may be typical to grasp at first, but it would get simpler as you explore more. Stack Overflow is also a useful resource for related queries.

Log in to reply.