Skip to Content
Menu

geoSuccessCallback()

Store geolocation data when successfully detected from the Google Maps API.

JavaScript February 7, 2021

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

This function triggers geolocationSuccess when finished so the nebula.session.geolocation can be used.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/location.js on line 201.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    function geoSuccessCallback(position){
        nebula.session.geolocation = {
            error: false,
            coordinates: { //A value in decimal degrees to an precision of 4 decimal places is precise to 11.132 meters at the equator. A value in decimal degrees to 5 decimal places is precise to 1.1132 meter at the equator.
                latitude: position.coords.latitude,
                longitude: position.coords.longitude
            },
            accuracy: {
                meters: position.coords.accuracy,
                miles: (position.coords.accuracy*0.000621371).toFixed(2),
            },
            altitude: { //Above the mean sea level
                meters: position.coords.altitude,
                miles: (position.coords.altitude*0.000621371).toFixed(2),
                accuracy: position.coords.altitudeAccuracy,
            },
            speed: {
                mps: position.coords.speed,
                kph: (position.coords.speed*3.6).toFixed(2),
                mph: (position.coords.speed*2.23694).toFixed(2),
            },
            heading: position.coords.heading, //Degrees clockwise from North
            address: false
        };
    
        nebula.session.geolocation.accuracy.color = '#ff1900';
        nebula.session.geolocation.accuracy.description = 'Very Poor (>1500m)';
        if ( nebula.session.geolocation.accuracy.meters < 50 ){
            nebula.session.geolocation.accuracy.color = '#00bb00';
            nebula.session.geolocation.accuracy.description = 'Excellent (<50m)';
        } else if ( nebula.session.geolocation.accuracy.meters > 50 && nebula.session.geolocation.accuracy.meters < 300 ){
            nebula.session.geolocation.accuracy.color = '#a4ed00';
            nebula.session.geolocation.accuracy.description = 'Good (50m - 300m)';
        } else if ( nebula.session.geolocation.accuracy.meters > 300 && nebula.session.geolocation.accuracy.meters < 1500 ){
            nebula.session.geolocation.accuracy.color = '#ffc600';
            nebula.session.geolocation.accuracy.description = 'Poor (300m - 1500m)';
        }
    
        gtag('set', 'user_properties', {
            geolocation_accuracy: nebula.session.geolocation.accuracy.description
        });
    
        nebula.addressLookup(position.coords.latitude, position.coords.longitude);
    
        sessionStorage['nebulaSession'] = JSON.stringify(nebula.session);
        nebula.dom.document.trigger('geolocationSuccess', nebula.session.geolocation);
        nebula.dom.body.addClass('geo-latlng-' + nebula.session.geolocation.coordinates.latitude.toFixed(4).replace('.', '_') + '_' + nebula.session.geolocation.coordinates.longitude.toFixed(4).replace('.', '_') + ' geo-acc-' + nebula.session.geolocation.accuracy.meters.toFixed(0).replace('.', ''));
        nebula.session.geolocation.coordinates.anonymized = nebula.session.geolocation.coordinates.latitude.toFixed(2) + ', ' + nebula.session.geolocation.coordinates.longitude.toFixed(2);
    
        gtag('set', 'user_properties', {
            geolocation: nebula.session.geolocation.coordinates.anonymized
        });
    
        gtag('event', 'geolocation', {
            event_category: 'Geolocation',
            event_action: 'Success',
            coordinates: nebula.session.geolocation.coordinates.anonymized,
            accuracy: nebula.session.geolocation.accuracy.meters.toFixed(2) + ' meters'
        });
    
        nebula.crm('identify', {'geolocation': nebula.session.geolocation.coordinates.latitude.toFixed(4) + ', ' + nebula.session.geolocation.coordinates.longitude.toFixed(4) + ' (Accuracy: ' + nebula.session.geolocation.accuracy.meters.toFixed(2) + ' meters'});
    }
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript
    function geoSuccessCallback(){
        //Write your own code here, leave it blank, or return false.
    }