Skip to content

Commit

Permalink
GeoBlacklight Google Anaytics Module
Browse files Browse the repository at this point in the history
Addresses #13
  • Loading branch information
ewlarson committed Mar 19, 2021
1 parent 9bc756c commit ad531db
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .example.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ MYSQL_SERVER=127.0.0.1
MYSQL_PORT=3306
MYSQL_USER=root
MYSQL_PASSWORD=root

# Google Analytics
GOOGLE_ANALYTICS=UA-XXXXXXXX-X
101 changes: 101 additions & 0 deletions app/assets/javascripts/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Inspired by and modified from http://railsapps.github.io/rails-google-analytics.html

GoogleAnalytics = (function() {
function GoogleAnalytics() {}

GoogleAnalytics.load = function() {
var firstScript, ga;
window._gaq = [];
GoogleAnalytics.analyticsId = GoogleAnalytics.getAnalyticsId();
window._gaq.push(['_setAccount', GoogleAnalytics.analyticsId]);
ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
firstScript = document.getElementsByTagName('script')[0];
firstScript.parentNode.insertBefore(ga, firstScript);
};

GoogleAnalytics.trackPageview = function(url) {
if (!GoogleAnalytics.isLocalRequest()) {
if (url) {
window._gaq.push(['_trackPageview', url]);
} else {
window._gaq.push(['_trackPageview']);
}
return window._gaq.push(['_trackPageLoadTime']);
}
};

GoogleAnalytics.isLocalRequest = function() {
return GoogleAnalytics.documentDomainIncludes('local');
};

GoogleAnalytics.documentDomainIncludes = function(str) {
return document.domain.indexOf(str) !== -1;
};

GoogleAnalytics.getAnalyticsId = function() {
return $('[data-analytics-id]').data('analytics-id');
};

return GoogleAnalytics;

})();

Blacklight.onLoad(function() {
GoogleAnalytics.load();
if (GoogleAnalytics.analyticsId) {
GoogleAnalytics.trackPageview();
}

// Log spatial search events

// Map Moved
History.Adapter.bind(window, 'statechange', function(e) {
var state = History.getState();
window._gaq.push(['_trackEvent', 'Spatial Search', 'Map Moved', state.url]);
});

// Initiate search in an area
$('.leaflet-control.search-control a.btn-primary').on('click', function(e) {
window._gaq.push(['_trackEvent', 'Spatial Search', 'Search Here', e.currentTarget.baseURI]);
});

// Log download clicks
$(document).on('click', '[data-download="trigger"]', function(e) {
var data = $(e.target).data();
window._gaq.push(['_trackEvent', 'Download', data.downloadId, data.downloadType]);
});

// Log failed download
$(document).on('DOMNodeInserted', '[data-download="error"]', function(e) {
var data = $(e.target).data();
window._gaq.push(['_trackEvent', 'Failed Download', data.downloadId, data.downloadType]);
});

// Log all show page tool clicks
$(document).on('click', 'div.show-tools li a', function() {
var data = $(this);
window._gaq.push(['_trackEvent', $.trim(data[0].innerText), window.location.href.split("/").pop()]);
});

// Log all facet expand/collapse clicks
$('#facets h3').on('click', function(){
var label = $(this).text();
var heading_values = ['Facets', 'collapse_expand', label];
_gaq.push(['_trackEvent'].concat(heading_values));
});

// Log all facet more link clicks
$('#facets li.more_facets a').on('click', function(){
var label = $(this).find("span").text();
var heading_values = ['Facets', 'more_link', label];
_gaq.push(['_trackEvent'].concat(heading_values));
});

// Log all item viewer definition rollovers
$('#viewer-container a[data-toggle="popover"]').hover(function(){
window._gaq.push(['_trackEvent', 'Item Viewer Popover', window.location.href.split("/").pop()]);
});
});
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
//= require geoblacklight

// UMass
//= require analytics
3 changes: 2 additions & 1 deletion app/views/layouts/blacklight/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;800&display=swap" rel="stylesheet">
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= javascript_include_tag 'analytics' %>
<%= csrf_meta_tags %>
<%= content_for(:head) %>
</head>
Expand All @@ -32,7 +33,7 @@
</div>
</div>

<main id="main-container" class="<%= container_classes %>" role="main" aria-label="<%= t('blacklight.main.aria.main_container') %>">
<main id="main-container" class="<%= container_classes %>" role="main" aria-label="<%= t('blacklight.main.aria.main_container') %>" data-analytics-id="<%= Rails.configuration.google_analytics %>">
<%= content_for(:container_header) %>
<%= render partial: 'shared/flash_msg', layout: 'shared/flash_messages' %>
Expand Down
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true

# Google Analytics - Dev
config.google_analytics = ENV['GOOGLE_ANALYTICS']

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

Expand Down
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

# Google Analytics - Prod
config.google_analytics = ENV['GOOGLE_ANALYTICS']

# Inserts middleware to perform automatic connection switching.
# The `database_selector` hash is used to pass options to the DatabaseSelector
# middleware. The `delay` is used to determine how long to wait after a write
Expand Down
3 changes: 3 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []

# Google Analytics - Test
config.google_analytics = ENV['GOOGLE_ANALYTICS']

# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
Rails.application.config.assets.precompile += %w( analytics.js )

Rails.application.config.assets.precompile += %w( favicon.ico )

Expand Down
7 changes: 4 additions & 3 deletions test/system/homepage_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ def setup
end

def test_basic_dom
assert page.has_selector?('header') # Global Header
assert page.has_selector?('header') # Header
assert page.has_selector?("[data-analytics-id]") # Google Analytics
assert page.has_selector?('ul.navbar-nav') # Navbar
assert page.has_selector?('footer') # Global Footer
assert page.has_selector?('footer') # Footer
end

def test_homepage_copy
within('ul.navbar-nav') do
within('#application-nav ul.navbar-nav') do
assert page.has_link?("About")
assert page.has_link?("Contact")
assert page.has_link?("Help")
Expand Down

0 comments on commit ad531db

Please sign in to comment.