I have set up Google Analytics tracking in my Rails 4 app with help from this SO answer. Page tracking is working just fine.
In my app, users can click on a link, and a Bootstrap modal will pop up. I want to track whenever the modal pops up, so I added this code to my js.erb file:
<% if Rails.env.production? %>
ga('send', 'pageview', '<%= "#{resource_path(@resource)}_modal" %>');
<% end %>
This is properly tracking all modal clicks, but now my regular page tracking doesn't seem to be working. Is there a way I can get both to work?
EDIT #1:
After doing some debugging with an alert
instead of a ga
call, I found out that all of my code was indeed being executed. Now I'm thinking the ga
object is having issues. Do I need to create a new object for Google Analytics in my js.erb file since it's not in the head of my HTML page with the rest of the Google Analytics code?
Any insight would be greatly appreciated.
EDIT #2:
I found this troubleshooting guide from Google, and it provides some tips on how to debug your website's analytics. The guide also contains a link to a Google Chrome extension called Google Analytics Tracking Code Debugger, which helps you troubleshoot tracking code issues. I'm using it now (it's great), and it seems that my regular page requests are not being tracked while my modal requests ARE going through. It seems like I have to make some code changes.
Well, after all that debugging, it seems that the precompiled assets weren't being updated on my production server for whatever reason. I'm not sure why they weren't being updated before, but after about 10 rake asset:clean
and rake asset:precompile
commands, the assets are now updated. Google Analytics is working great for all page views and modal clicks now!