FR: Expose results from HTML5 Geolocation API to AMP
Expose results from HTML5 Geolocation API to AMP: e.g. for bindings to amp-list[src], once the user has consented, give developers the ability to send lat/long information along with the server call so as to provide personalized results in response, based on geo
/cc @aghassemi @choumx
/cc @dandv (this is a narrow version of your earlier issue, to specify it just to be the API itself, separate from larger questions of geo-variation on AMP caches)
One idea is to expose these through variable substitution: e.g. GEO_LAT, GEO_LON
Since these values are not always available, values could be empty. First time AMP runtime sees these variables, that's when user will be prompted for granting permission (variable substitution supports async values, so we don't need to wait for another page load after permission is granted to do substitution)
@dvoytenko thoughts? @choumx (somewhat related): do you have plans to expose our existing variables to bind? Would be nice to have access to them as amp-bind variables as well for greater flexibility.
The user permission seems a pretty drastic ask to personalize a generic amp-list? Typically this would be done as a coarse fidelity via IP address.
Technically, some issues we'd run with when AMP is embedded in an iframe - which is faire amount of impressions. The only way would be to ask the viewer I believe, but I don't see how the viewer can pass it on to the document properly.
For context: the rationale for this request was because IP address wasn't offering enough fidelity for their purposes.
That's a good point about the viewer — does sound like a pretty big hurdle
If we were to proceed, I can imagine we could have an extension that would request geo permissions when the page is executed on the direct origin - this could be useful for AMP as canonical and other cases. But we'd disable this extension when executed from proxy. Which means, GEO_ vars would also have to "understand" that they may not be available.
@dvoytenko Geo location permission can be requested by iframes (tested with Android and iOS) so I don't believe we need viewer integration for this (which is a good thing considering with CURLS user is giving the permission to a single publisher and not everyone in AMP)
@aghassemi There are two problems with this:
- The origin requesting permission will be that of proxy cache. That might not be clear for a user.
- I believe both browsers are considering deprecating geo location in 3p iframes.
Hello. Just bumping this up again to see if this can be prioritized. As a larger corpus of AMP pages start to be built, this feature seems like a requirement to ensure continued adoption, especially as the pages built are deeper into the funnel. IP Based geolocation is limited in its accuracy and this approach puts the control back in the hands of the user/creator.
Also, is the iframe based approach still viable i.e. will the iframe trigger a request to the user asking for permissions when the page is rendered from the amp cache? This is less desirable given the limitations of amp-iframe but this may be the only option until this feature is built out?
I know that the amp-geo component does give country level detection. Could that functionality be extended to provide more granular geolocation information?
Thanks @shishirm — it's unclear still if the Geolocation API is exactly the method that should be used for these use cases, but we are doing a deeper dive into geo- (and other types of) personalization this quarter, and would love to hear some specific use cases that we can use to develop solutions. Please feel free to share
Following comment is in response to PR https://github.com/ampproject/amphtml/pull/18561, posting here to keep the discussion on the issue instead of a PR.
Hi @eduardogoncalves
Lots of the discussions here are from before amp-geo component was created by @jpettitt. At the moment, amp-geo covers a lot of the use-cases but precise lon/lat to be passed to amp-iframe or amp-list is missing so trying to solve that problem is definitely in the right direction.
I personally think this should become a feature of the existing amp-geo component instead of a new one. @jpettitt were there any discussions around using geolocation API to provide precise location as part of amp-geo design?
I think this needs to be discussed in a Design Review Meeting but below are my thoughts on couple of options:
Option 1
Add a new getCurrentPosition() ACTION to amp-geo without impacting how amp-geo works currently in any other way.
- Hight-trust action that can only be triggered via user-action
- It will be a promise that blocks the
action chainuntil resolved - Browser will prompt the user for permission when action is called (if user has not given permission already)
- When resolved it will either append
lon/latto existingampGeostate (if"AmpBind": truewas specified in the config ofamp-geo) or it will create theampGeostate with onlylon/lat - If user denies permission, Action's promise is still resolved but
lon/latwill be set toNaN(or 0?). This will allow the developer to handle the user rejection case properly (as opposed to not resolving the promise which means pending and rejected states can not be distinguished)
Pros
- Does not change how
amp-geoworks in anyway.pendingstate ofamp-geois still tied to country detection using IP. It simply provides an API intogeolocation.getCurrentPositionas part ofamp-geocomponent. - User can never be promoted for permission during load time.
Cons
- Does not handle load-time dynamic content (e.g
amp-listneeds to be personalized based on precise location at load time). This is in contract to howamp-geoworks with approximate location where VARIABLE_SUBSTITUTION and CSS are allowed to target country during load time.
Option 2
Add a new usePreciseLocation: true option to amp-geo's configuration.
- In short, this will prevent
amp-geofrom server-side rendering but conceptually it is compatible with existingamp-geoAPI, it just add an additionalrequest for permission and get locationpromise to the chain beforeamp-geois resolved. amp-geowill stay inpendingstate until user has given the permission.amp-geowould sendlog/latto the server to be used for country detection instead of IP.amp-geowill additionally addlog/latto theampGeo state.- We would need to expose
errororrejectedstate concept toamp-geowhich it does not currently support.
Cons (if usePreciseLocation: true option is used)
- user will be promoted for permission at load time (bad UX :( )
amp-geocan not be SSRedamp-geocan not be resolved during pre-rendering unless user has already given permission- combining this with render-blocking can be super bad UX, specially when user has not given permission yet (i.e. imagine a blank page asking for location permission)
Pros
- Allows for personalization at load time based on precise geo-location same way (with the caveat above)
amp-geocurrently allows with approximate geo-location.
/cc @ericlindley-g @jpettitt @choumx thoughts?
@aghassemi Can we check at load-time if user has been prompted to allow/deny geolocation?
navigator.permissions.query({'name': 'geolocation'})
.then( permission => console.log(permission) )
that can show:
>>> PermissionStatus {state: "prompt", onchange: null}
>>> PermissionStatus {state: "granted", onchange: null}
>>> PermissionStatus {state: "denied", onchange: null}
- If user has not been prompted yet, we wait until user taps a button;
- if user has already allowed we get lat/log;
- if user has denied we bind this info so the component that needs lat/log can use it's defaults.
@eduardogoncalves load-time is instant in AMP due to Server-side rendering, without making an extension render-blocking, by the time we get to navigator.permissions.query, page is already shown.
I did look very briefly at using the geolocation API in amp-geo. The reasons I didn't use it include:
- For the specific use case, country level customizations for privacy permissions, asking for privacy centric data (precise location) would have been counter productive.
- The need to prompt the user and the questions around the scope of the permission. For example if an iframed AMP page requests and received geolocation permissions does that permission carry over other pages iframed by the same origin which would be undesirable.
- Undesirable render blocking while waiting for permission
- Ambiguity about who was requesting permission
Hi, any news on "expose results from HTML5 Geolocation API"? I think the last discussion on this was on https://github.com/ampproject/amphtml/issues/18658#issuecomment-434853467
Will we create a new component or use amp-geo? Or we won't support getting user's current position provided by HTML5 Geolocation API window.navigator.geolocation.getCurrentPosition()?
Hi,
Sorry for the late reply. We did a design review on this and a strong requirement was only allowing this on user-interaction. We don't want the prompt on page load.
We can have this feature by just adding a new action to amp-geo called getPreciseLocation which would trigger an event on amp-geo ( preciseLocation). When data is available. Event would have the lat and lon. No need for amp-state integration as user can setState as result of the event.
I'd love to learn more about this feature request. Is there more information available estimated launch date for getPreciseLocation?
Hi @safwans
@cvializ has started working on this. Plan to launch by end of June.
We would love to hear your use-case and how you imagine to use this feature if you don't mind sharing. /cc @jeffjose
That's great news!
The use case in point is search results based on current location. With this feature, our partners can suggest listings that are closer to the user's current location.
Thanks for the extra context, @safwans.
I think your usecase would be covered in the work that @cvializ is doing.
this has been implemented as amp-user-location see https://amp.dev/documentation/components/amp-user-location for details.