ngx-google-places-autocomplete icon indicating copy to clipboard operation
ngx-google-places-autocomplete copied to clipboard

How to pass a sessionToken to my request?

Open jaradehr1 opened this issue 5 years ago • 7 comments

The Per Request is the default billing option when you use AutocompleteService(). Charges are applied per keystroke, that could lead to higher billings. I am trying to use Session Tokens when implementing AutocompleteService.getPlacePredictions() to group together autocomplete requests for billing purposes.

Session tokens group the query and selection phases of a user autocomplete search into a discrete session for billing purposes. The session begins when the user starts typing a query, and concludes when they select a place. Each session can have multiple queries, followed by one place selection.

Please see the JS code

// Create a new session token.
let sessionToken = new google.maps.places.AutocompleteSessionToken();
// Pass the token to the autocomplete service.
let autocompleteService = new google.maps.places.AutocompleteService();
autocompleteService.getPlacePredictions({
  input: user_input,
  sessionToken: sessionToken
}, suggestions);

The handleAddressChange(address: Address) fires on selection, but there is no other event that fires per keystroke

Any help how to do this in my Angular 8? Please, refer to my stackoverflow question

jaradehr1 avatar Nov 15 '20 22:11 jaradehr1

It would be great to know if this is an issue with this plugin. If we could all safe some money I'd also be ready to do a PR :-)

tobika avatar Dec 14 '20 15:12 tobika

https://developers.google.com/maps/documentation/javascript/places-autocomplete

It seems that sessionToken is only used with the getPlacePredictions and not getPlace that is used in this project.

tobika avatar Dec 14 '20 15:12 tobika

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

IslombekHasan avatar Jan 13 '21 15:01 IslombekHasan

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

Correct, they are automatically handled. However, the autocomplete widget does not give you control over how to use that session which leads to a higher bill.

jaradehr1 avatar Jan 18 '21 18:01 jaradehr1

If I can get your input on this guys:

https://stackoverflow.com/questions/64864286/how-to-implement-session-tokens-properly-with-google-autocomplete-service-api-in

jaradehr1 avatar Jan 18 '21 18:01 jaradehr1

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

Correct, they are automatically handled. However, the autocomplete widget does not give you control over how to use that session which leads to a higher bill.

Indeed. Though, I wonder, do we need that control? If you test the widget and have a look at the Network tab, you'll see that AutocompletionService.GetPredictions method is fired together with a query that has a parameter&20s. Right after this comes a token, which is reused in all other GetPredictions requests until you fire a GetPlaceDetails by choosing a place. I checked this several times and it was a relief to know that.

IslombekHasan avatar Jan 19 '21 08:01 IslombekHasan

Some more insight on this. Since this plugin uses an Autocomplete widget, session tokens are automatically handled in the background. Please refer to this link. https://developers.google.com/maps/documentation/javascript/places-autocomplete#session_tokens

Correct, they are automatically handled. However, the autocomplete widget does not give you control over how to use that session which leads to a higher bill.

Indeed. Though, I wonder, do we need that control? If you test the widget and have a look at the Network tab, you'll see that AutocompletionService.GetPredictions method is fired together with a query that has a parameter&20s. Right after this comes a token, which is reused in all other GetPredictions requests until you fire a GetPlaceDetails by choosing a place. I checked this several times and it was a relief to know that.

hmm.. I think I read in the documentation that every key stroke is considered an api call with (different session token). That is what the implementation of Autocomplete widget that uses getPlace function.. with get predication, you get to choose to use the same token over and over with every key stroke until the user selects a place. Correct me if I am wrong.

jaradehr1 avatar Jan 29 '21 16:01 jaradehr1