Skip to content

Commit

Permalink
Track clicks on Follow/Unsubscribe buttons with Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
equivalentideas committed Oct 10, 2016
1 parent d06767b commit 108c9d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions assets/javascripts/event_tracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$(document).ready(function() {
// check if the Google Analytics function is available
if (typeof ga == 'function') {
function trackFollow() {
ga('send', 'event', 'following', 'click Follow buttom');

This comment has been minimized.

Copy link
@equivalentideas

equivalentideas Oct 10, 2016

Author

Typo here. “Buttom

};

function trackUnsubscribe() {
ga('send', 'event', 'following', 'click Unsubscribe buttom');
};

$('.track-request-action').click(function(e) { trackFollow(); });
$('.track__action').click(function(e) { trackFollow(); });

$('.unsubscribe__action').click(function(e) { trackUnsubscribe() });
$('.unsubscribe-request-action').click(function(e) { trackUnsubscribe() });
}
})
1 change: 1 addition & 0 deletions lib/views/general/_before_head_end.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<link href='https://fonts.googleapis.com/css?family=Maven+Pro:400,700%7CActor' rel='stylesheet' type='text/css'>
<%= javascript_include_tag 'event_tracking' unless AlaveteliConfiguration::ga_code.empty? || (@user && @user.super?) %>

4 comments on commit 108c9d9

@equivalentideas
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This broke production:

ActionView::Template::Error (event_tracking.js isn't precompiled):
    1: <link href='https://fonts.googleapis.com/css?family=Maven+Pro:400,700%7CActor' rel='stylesheet' type='text/css'>
    2: <%= javascript_include_tag 'event_tracking' unless AlaveteliConfiguration::ga_code.empty? || (@user && @user.super?) %>
  lib/themes/righttoknow/lib/views/general/_before_head_end.html.erb:2:in `_lib_themes_righttoknow_lib_views_general__before_head_end_html_erb___617197513_118481350'

It wasn't a problem in development because production is configured to not fall back to the asset pipeline if an asset isn't precompiled, where as development is not. I believe this is because the file isn't in the precompile array.

Could this be an issue with Alaveteli not precompiling javascripts in the theme assest directory like it does for stylesheets? @garethrees am I just doing this in the wrong way? I’d really appreciate your advice on this :) Adding a script like this is how I interpreted Alaveteli wants us to based on this bit of the Theme docs http://alaveteli.org/docs/customising/themes/#changing-other-styling .

@garethrees
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't a problem in development…

Yeah, that's not ideal behaviour. Sounds like its still a mess.

I believe this is because the file isn't in the precompile array.

Yeah, that'll be it. I think you should be able to add it to the precompile array in your alavetelitheme.rb, after the theme assets have been added to the load path:

Rails.application.config.assets.precompile += ['event_tracking.js']

We don't appear to have any themes that add custom JS like this, so I guess you'll be the pioneers of this!

@equivalentideas
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @garethrees I'll pioneer away then 😀

@equivalentideas
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the trick @garethrees 3c85154

I could add that to the docs, or write something to include any .js put into /assets/javascripts . That would make the current docs correct and probably be easier for people. You still need to link to these files in your app, so we wouldn't be automatically serving stuff to users, which was a concern for me.

Please sign in to comment.