Skip to content

Instantly share code, notes, and snippets.

@salsalabs
Last active May 5, 2017 17:09
Show Gist options
  • Save salsalabs/fa17ef4a87972d68631a to your computer and use it in GitHub Desktop.
Save salsalabs/fa17ef4a87972d68631a to your computer and use it in GitHub Desktop.
Send donatoin_KEY and amount to Google Analytics (GA) from the thank you page that follows a successful donation in Salsa.
<script>
// Script to retrieve donation_KEY and amount from the URL and send
// them to Google Anaylitics eCommerce.
// See https://salsasupport.zendesk.com/entries/98811317
var m = /donation_KEY=(\d+)/.exec(window.location.search);
var donation_KEY = (m != null) ? donation_KEY = m[1] : 'Unknown';
var m = /amount=(\d+)/.exec(window.location.search);
var amount = parseFloat(window.location.href.split("amount=")[1].split('&'));
(function(i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function() {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date();
a = s.createElement(o),
m = s.getElementsByTagName(o)[0];
a.async = 1;
a.src = g;
m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', YOUR_GA_KEY, 'auto', {
'allowLinker': true
});
ga('require', 'linker');
ga('linker:autoLink', [YOUR_DOMAIN]);
ga('require', 'ecommerce');
ga('ecommerce:addTransaction', {
'id': donation_KEY,
'affiliation': 'Donation',
'revenue': amount,
'shipping': '',
'tax': '',
'currency': 'USD' // local currency code.
});
ga('ecommerce:send');
ga('send', 'pageview');
</script>
@rxnlabs
Copy link

rxnlabs commented May 5, 2017

This script above is great but causes a JS error to be thrown if the amount= is not in the URL. The revision below checks to make sure that the amount= is in the URL before attempting to get the donation amount:

<script>
// Script to retrieve donation_KEY and amount from the URL and send
// them to Google Anaylitics eCommerce.
// See https://salsasupport.zendesk.com/entries/98811317
var m = /donation_KEY=(\d+)/.exec(window.location.search);
var donation_KEY = (m != null) ? donation_KEY = m[1] : 'Unknown';
var m = /amount=(\d+)/.exec(window.location.search);
if ( document.location.href.indexOf('amount=') !== -1 ) {
    var amount = parseFloat(window.location.href.split("amount=")[1].split('&'));
    (function(i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r;
        i[r] = i[r] || function() {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date();
        a = s.createElement(o),
            m = s.getElementsByTagName(o)[0];
        a.async = 1;
        a.src = g;
        m.parentNode.insertBefore(a, m)
    })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
    ga('create', YOUR_GA_KEY, 'auto', {
        'allowLinker': true
    });
    ga('require', 'linker');
    ga('linker:autoLink', [YOUR_DOMAIN]);
    ga('require', 'ecommerce');
    ga('ecommerce:addTransaction', {
        'id': donation_KEY,
        'affiliation': 'Donation',
        'revenue': amount,
        'shipping': '',
        'tax': '',
        'currency': 'USD' // local currency code.
    });
    ga('ecommerce:send');
    ga('send', 'pageview');
}
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment