Skip to Content
Menu

socialSharing()

Create social sharing URLs on specific links.

JavaScript February 26, 2024

Usage

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

Additional Notes

This function looks for the classes facebook-share (Facebook), twitter-share (Twitter), linkedin-share (LinkedIn), pinterest-share (Pinterest), and email-share (Email) to replace the href with their appropriate “sharer” intent URL (and parameters).

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/social.js on line 20.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.socialSharing = async function(){
        if ( jQuery('[class*="share"]').length ){
            const encloc = encodeURIComponent(window.location.href);
            const enctitle = encodeURIComponent(document.title);
            let popupTop = nebula.dom.window.height()/2-275;
            let popupLeft = nebula.dom.window.width()/2-225;
            let popupAttrs = 'top=' + popupTop + ', left=' + popupLeft + ', toolbar=0, location=0, menubar=0, directories=0, scrollbars=0, chrome=yes, personalbar=0';
    
            //These events will need to correspond to the GA4 event name "share" and use "content_type" and "item_id" as parameters: https://support.google.com/analytics/answer/9267735
    
            //Facebook
            jQuery('a.facebook-share, .facebook-share a, a.nebula-share.facebook, .nebula-share a.facebook').attr('href', 'http://www.facebook.com/sharer.php?u=' + encloc + '&t=' + enctitle).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
                let thisEvent = {
                    event: e,
                    event_name: 'share',
                    event_category: 'Social',
                    event_action: 'Share',
                    network: 'Facebook',
                    url: window.location.href,
                    title: document.title
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
                nebula.crm('event', thisEvent.network + ' ' + thisEvent.event_action);
    
                if ( nebula.dom.body.hasClass('desktop') ){
                    window.open(jQuery(this).attr('href'), 'facebookShareWindow', 'width=550, height=450, ' + popupAttrs);
                    return false;
                }
            });
    
            //Twitter
            jQuery('a.twitter-share, .twitter-share a, a.nebula-share.twitter, .nebula-share a.twitter').attr('href', 'https://twitter.com/intent/tweet?url=' + encloc + '&text=' + enctitle).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
                let thisEvent = {
                    event: e,
                    event_name: 'share',
                    event_category: 'Social',
                    event_action: 'Share',
                    network: 'Twitter',
                    url: window.location.href,
                    title: document.title
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
                nebula.crm('event', thisEvent.network + ' ' + thisEvent.event_action);
    
                if ( nebula.dom.body.hasClass('desktop') ){
                    window.open(jQuery(this).attr('href'), 'twitterShareWindow', 'width=600, height=254, ' + popupAttrs);
                    return false;
                }
            });
    
            //LinkedIn
            jQuery('a.linkedin-share, .linkedin-share a, a.nebula-share.linkedin, .nebula-share a.linkedin').attr('href', 'http://www.linkedin.com/shareArticle?mini=true&url=' + encloc + '&title=' + enctitle).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
                let thisEvent = {
                    event: e,
                    event_name: 'share',
                    event_category: 'Social',
                    event_action: 'Share',
                    network: 'LinkedIn',
                    url: window.location.href,
                    title: document.title
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
                nebula.crm('event', thisEvent.network + ' ' + thisEvent.event_action);
    
                if ( nebula.dom.body.hasClass('desktop') ){
                    window.open(jQuery(this).attr('href'), 'linkedinShareWindow', 'width=600, height=473, ' + popupAttrs);
                    return false;
                }
            });
    
            //Pinterest
            jQuery('a.pinterest-share, .pinterest-share a, a.nebula-share.pinterest, .nebula-share a.pinterest').attr('href', 'http://pinterest.com/pin/create/button/?url=' + encloc).attr({'target': '_blank', 'rel': 'noopener'}).on('click', function(e){
                let thisEvent = {
                    event: e,
                    event_name: 'share',
                    event_category: 'Social',
                    event_action: 'Share',
                    network: 'Pinterest',
                    url: window.location.href,
                    title: document.title
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
                nebula.crm('event', thisEvent.network + ' ' + thisEvent.event_action);
    
                if ( nebula.dom.body.hasClass('desktop') ){
                    window.open(jQuery(this).attr('href'), 'pinterestShareWindow', 'width=600, height=450, ' + popupAttrs);
                    return false;
                }
            });
    
            //Email
            jQuery('a.email-share, .email-share a, a.nebula-share.email, .nebula-share a.email').each(function(){
                let emailSubject = jQuery(this).attr('data-subject') || document.title; //Use the page title unless a data attribute for the subject exists (Note that we are not using encoded values here)
                let emailBody = jQuery(this).attr('data-body') || window.location.href; //Use the page URL unless a data attribute for the body exists (Note that we are not using encoded values here)
                jQuery(this).attr('href', 'mailto:?subject=' + encodeURIComponent(emailSubject) + '&body=' + encodeURIComponent(emailBody)).attr({'target': '_blank', 'rel': 'noopener'});
            }).on('click', function(e){
                let thisEvent = {
                    event: e,
                    event_name: 'share',
                    event_category: 'Social',
                    event_action: 'Share',
                    network: 'Email',
                    url: window.location.href,
                    title: document.title
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
                nebula.crm('event', thisEvent.network + ' ' + thisEvent.event_action);
            });
    
            //Web Share API: https://caniuse.com/mdn-api_navigator_share
            if ( 'share' in navigator ){ //Chrome 61+
                nebula.dom.document.on('click', 'a.shareapi, .shareapi a, a.nebula-share.shareapi, .nebula-share a.shareapi', function(e){
                    let thisEvent = {
                        event: e,
                        event_name: 'share',
                        event_category: 'Social',
                        event_action: 'Share',
                        network: 'Share API (Drawer Opened)',
                        url: window.location.href,
                        title: document.title,
                    };
    
                    nebula.dom.document.trigger('nebula_event', thisEvent);
                    gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
    
                    let $oThis = jQuery(this);
                    let originalText = $oThis.html();
    
                    navigator.share({
                        title: document.title,
                        text: nebula.post.excerpt,
                        url: window.location.href
                    }).then(function(){
                        let thisEvent = {
                            event: e,
                            event_name: 'share',
                            event_category: 'Social',
                            event_action: 'Share',
                            network: 'Share API (Success)',
                            url: window.location.href,
                            title: document.title,
                        };
    
                        nebula.dom.document.trigger('nebula_event', thisEvent);
                        gtag('event', thisEvent.event_name, nebula.gaEventObject(thisEvent));
                        nebula.crm('event', thisEvent.network);
                        $oThis.addClass('success');
                        nebula.createCookie('shareapi', true);
                    }).catch(function(error){ //This can happen on iOS when the user closes the drawer without sharing
                        gtag('event', 'exception', {
                            message: '(JS) Share API Error: ' + error,
                            fatal: false
                        });
                        $oThis.addClass('error').html(originalText);
                        nebula.createCookie('shareapi', false);
                    });
    
                    return false;
                });
    
                nebula.createCookie('shareapi', true); //Set a cookie to speed up future page loads by not loading third-party share buttons.
            } else {
                jQuery('a.shareapi, .shareapi a, a.nebula-share.shareapi, .nebula-share a.shareapi').addClass('hidden');
            }
        }
    };

    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

    For non-module import functions:

    nebula.socialSharing = function(){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.socialSharing = function(){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.socialSharing === 'function' ){
            nebula.socialSharing = function(){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });