Business

How We Reduced Cloud Spends of Google Place Autocomplete by 80% - A Case Study

How we Reduced Cloud Spends of Google Place Autocomplete by 80% - A Case Study
How we Reduced Cloud Spends of Google Place Autocomplete by 80% - A Case Study

Running a startup means finding ways to save money wherever possible. Big-name services like Google and Amazon are essential but can be pricey. In this case study, we share how we dramatically lowered the cost of using Google Place Autocomplete. We'll break down the steps we took, the challenges we faced, and how we achieved an incredible 80% reduction in spending. Dive into our story to learn practical tips for saving money while still getting the services you need to grow your startup.

What is a Google Place Autocomplete API

This Google API returns location suggestions based on a given phrase. It works in the same way as the Google Maps search input.

google place autocomplete API

Why we are using locations autocompletion?

One of our e-commerce partners has a product that allows users to sell and buy items. Buyers are often looking for offers that are near their homes. If we want to display products that are nearby we need their coordinates. By using Place Autocomplete API we can show products in the near distance.

On the other hand, buyers tend to order a parcel at pickup points. Users have to specify at which one they want to collect a package. This is possible due to a map that displays pickup points and has autocomplete locations input so that buyers don’t have to scroll through a whole country and find the desired place. And again this is done with Google Place Autocomplete API. But here we didn’t have control over this map, we were using an external widget.

What was the problem?

Google has great location services that are pretty straightforward but with ease of use comes responsibility - especially when it comes to costs. For example, we observed that we were requesting too much not needed data. For our purpose, we are only using location coordinates but in response, we had photos of a place, ratings, opening hours, etc. Those data are additionally billed. Moreover, on every keystroke in the input, the Autocomplete request was made. We were charged for every keyed-in character.

Finding the best solution

We had some ideas on how to reduce costs but we stick to Google services because of our expertise in this field. So how did we find out that this is possible to get some cost optimizations in the current usage of Google services? First of all, it’s good to know how gathering coordinates from an autocomplete input works.

Google makes this possible by using two services. When a user types in a phrase in an input, there is a request to a Place Autocomplete API which returns suggestions. There can be continuously more requests as a user types more characters. When finally is selected a specific location then we receive coordinates from the Place Details API. So there were many calls to the Autocomplete API and only one to the Place Details API.

Google Place autocomplete API requests

As seen in the above scenario, a user wants to find a “Los Angeles Airport”. Then starts typing the first letters of the word “Los” and receives suggestions that match the given phrase. So there were three requests to Autocomplete API and finally, there was only one request to the Place Details API. So does Google bill for every four requests in total? Not really. In fact, Google allows to combine all these four requests into only one in the billing.

Implementing sessionToken

Google has many APIs and they can be recognized by a Stock keeping unit (abbreviated SKU). This can clearly recognize a product and the price of every request. When it comes to Autocomplete API, an SKU is called “Autocomplete (included with Place Details) – Per Session”. In this approach, every Autocomplete request which eventually resolves to a Place Details request within a session costs $0. So getting back to our example with “Los Angeles Airport”, all three Autocomplete requests won’t be billed in the Google console. Doesn’t matter how many suggestions a user will receive, there can be even dozens of them. The only thing for which we have to pay is the call to a Place Details API.

So if we want to bind these requests together to reduce costs then it’s recommended to use a sessionToken. This way Google knows how to charge for an Autocomplete request included with Place Details request. The session starts as soon as user types in the first character and ends when selects a suggestion. It’s the developer's duty to regenerate these tokens only when crucial.

This snippet of code creates a request to an Autocomplete service and as a result, receives phrases that match to the text given in input. Notice how the sessionToken is used here.

const autocompleteService = new google.maps.places.AutocompleteService();

const getAutocompleteSuggestions = (
    input: string,
  sessionToken: google.maps.places.AutocompleteSessionToken
) => {
  autocompleteService.getPlacePredictions(
    {
      input,
      sessionToken,
    },
    (result: Array<{ place_id: string, description: string }>) => {
            // Here handle suggestions
    }
  );
};

Then we have to use the same sessionToken in the Place Details API request. This API requires placeId as a parameter - it’s in the result array received in the previous request.

const map = new google.maps.Map(document.querySelector('#map'));
const placesService = new google.maps.places.PlacesService(map);

const getPlaceDetails = (
  placeId: string,
  sessionToken: google.maps.places.AutocompleteSessionToken
) => {
  placesService.getDetails(
    {
      placeId,
      sessionToken,
            fields: ['geometry.location']
    },
    (result) => {
      // Here handle place details
    }
  );
};

First results

We implemented sessionToken and noticed that we have huge cost optimizations compared to the previous month. This was the first iteration that we’ve made. We did it only on a form where users post an offer and fill pickup location. By doing this we made almost 40% cost reductions.

first results of Google Places cost reduction

Meanwhile, we had business-related changes and we noticed that the current pickup points map widget didn’t ship the features we needed so we decided to create our own. We created a map with custom markers and reused a location's Autocomplete input. We used the same optimization techniques and something more.

Define what fields you will use

Have you noticed previously in the placesService.getDetails function that there is a fields argument? It’s very important to know what data is needed because if we won’t specify which ones will be used then all of them are fetched. All these fields are not for free. Actually, Google in Place Details API has three groups and each of them is billed differently. They name them Basic, Atmosphere, and Contact. In our example, if we want only coordinates we have to pass geometry.location. This is located under Basic data and it's not additionally charged, so it's included in the price of a single Place details API request.

placesService.getDetails(
  {
    placeId,
    sessionToken,
    fields: ['geometry.location'],
  },
  (result) => {
    // Here you can only lookup result.geometry.location
  }
);

Define suggestions restrictions

The e-commerce platform that we are working on is only available for users in a single country. They cannot place orders from foreign places. So users don’t need to see suggestions from other countries. This way we can omit selected by accident locations. Users are getting more accurate results.

autocompleteService.getPlacePredictions(
  {
    input,
    sessionToken,
    componentRestrictions: {
            // ISO 3166-1 Alpha-2 country code, case insensitive
            country: 'US'
        },
  },
  (result) => {
    // Here handle suggestions
  }
);

Final results

We made again huge billing reductions, comparing month to month it’s 80% cost optimization!

final results

At Appunite, we believe in more than just providing services; we're committed to generating real business impact for our clients. Our case study on reducing spending on Google Place Autocomplete by 80% showcases not only our cost-saving prowess but also our collaborative approach and unwavering focus on driving tangible results. As your trusted partner, we're here to support your success from day one, ensuring that together, we achieve excellence and maximize business outcomes.