Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytics for outbound links to learn what external resources are used #329

Open
Frijol opened this issue Jan 7, 2022 · 1 comment · Fixed by #334
Open

Analytics for outbound links to learn what external resources are used #329

Frijol opened this issue Jan 7, 2022 · 1 comment · Fixed by #334

Comments

@Frijol
Copy link
Contributor

Frijol commented Jan 7, 2022

It would be interesting to know if/which links people click on the External Resources page (https://environmentalenforcementwatch.org/data/resources/). If we add the following to our footer, I think they'll show up in Google Analytics.

(h/t https://www.joehessert.com/seo/google-analytics-external-link-tracking which has some other good ideas too)

//Track Outbound Link Clicks

(function trackOutbounds() {
        
        var hitCallbackHandler = function(url,win) {
            if (win) {
                    window.open(url, win);
            } else {
                window.location.href = url;
        }
    };
    
    var addEvent = function(el, eventName, handler) {
    
                if (el.addEventListener) {
                        el.addEventListener(eventName, handler);
                        } else {
                        el.attachEvent('on' + eventName, function(){
                                handler.call(el);
                    });
                }
        }
        
        if (document.getElementsByTagName) {
                var el = document.getElementsByTagName('a');
                var getDomain = document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0];
                
                // Look thru each a element
                for (var i=0; i < el.length;i++) {
                
                        // Extract it's href attribute
                        var href = (typeof(el[i].getAttribute('href')) == 'string' ) ? el[i].getAttribute('href') : '';
                        
                        // Query the href for the top level domain (xxxxx.com)
                        var myDomain = href.match(getDomain);
                        
                        // If link is outbound and is not to this domain        
                        if ((href.match(/^(https?:|\/\/)/i)  && !myDomain) || href.match(/^mailto\:/i)) {
                        
                                // Add an event to click
                                addEvent(el[i],'click', function(e) {
                                        var url = this.getAttribute('href'), win = (typeof(this.getAttribute('target')) == 'string') ? this.getAttribute('target') : '';
                                                        
                                                        console.log ("add event", url);
                                        // Log even to Analytics, once done, go to the link
                                        ga('send', 'event', 'outbound', 'click', url,
                                                {'hitCallback': hitCallbackHandler(url,win)},
                                                {'nonInteraction': 1}
                                        );
                                        
                                        e.preventDefault();
                                });
                        }
                }
        }
})();

@Frijol Frijol linked a pull request Jan 11, 2022 that will close this issue
@Frijol Frijol reopened this Jan 11, 2022
@Frijol
Copy link
Contributor Author

Frijol commented Jan 11, 2022

build error, hotfixing in #335

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant