Ahoy is not recording events or visits - ruby-on-rails

I have installed Ahoy to give some user analytics on my rails application. I am running rails 4.1.10 and using Postgres 9.3.10
I have added the gem to my gemfile:
gem 'ahoy_matey'
And I have run :
rails generate ahoy:stores:active_record -d postgresql
rake db:migrate
As per the docs.
I also added //=require ahoy after jquery in the application.js assets file.
I was not sure if visits and events would be tracked automatically - so I went to a couple of pages and checked manually in the DB and I see no entries in the visits or ahoy_events tables. So I manually coded this into my landing page index controller action:
ahoy.track "Viewed Landing", title: "Landing page viewed"
And this line causes the following error:
NameError in LandingPageController#index
undefined local variable or method `ahoy' for #<LandingPageController:0x007fa634fee7f0>
(Listed in full here : https://gist.github.com/renegadeandy/835a7fb0db2bb9d8ea95)
Can anybody help me get ahoy logging visits and events please! Ideally I want it to track everything, automatically if there is some golden switch I need to press :)

The problem was that the server had not been restarted :'(
Closing the server and reopening it - solved the problem!

Related

How to access heroku postgres Database with ruby on rails in shopify app

Hello I am working on a shopify embedded app with ruby on rails and I must save some config parameters given by the user or me.
I couldn't find any solution other than using heroku postgres database.
I looked everywhere for a code to insert or select data from the heroku database but I was unsuccessful in the Doc they literally gave one line of code in Ruby section.
https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-ruby
in the Gemfile add :
gem 'pg'
I have no idea how to execute any Query on the database with ruby in my controller.
EDIT :
I did all the configuration for the pg gem all I need to know is some example of code on how to use it
Use case example:
The app home have Input and Save button.
User 1 Enter data in input and Click save.
User 1 close the app.
User 1 returns to the app.
User 1 finds the data he wrote in the input.
Please help.
It would be more usual to save configuration info as heroku config vars, rather than in Postgres.
heroku config will list the current vars and heroku config:set VAR_NAME=var_value will set vars. This is not Ruby code - it is a part of heroku setup.
If you truly want to write Ruby on Rails code to modify the database, you need to learn ActiveRecord
Solved It with the help of reddit community.
All I had to do is adding this in my controller:
#mydb = PG.connect(ENV['DATABASE_URL'])
Query Example
#mydb.exec('insert into table values (\'test2\',\'test2\',\'2\');')

Rails 5 html changes not showing even after clearing each cache

I am new to RoR and using rails5 on windows and used Devise Gem for authentication. I have to integrate HTML design for admin section. The problem is that whatever changes that i am making in /app/views/admins/sessions/new.html.erb file, they are not reflecting in the browser. I always see default login form of devise in which it shows Email and Password field with shared links.
Event by removing entire html from above new.html.erb file, i can still see login form.
I have tried executing below commands to clear cache but still i can not see my new html changes in the browser.
rake tmp:cache:clear
rake assets:clean
Not sure what else is required to make it working. Any help would be greatly appreciated
Running rails generate devise:views will generate all of the views that Devise uses. Editing the generated sessions/new.html.erb file should update the version you are seeing in the browser.
You can also delete any of the other generated files that you don't need to override.
Source:
https://github.com/plataformatec/devise#configuring-views

pdf.js gem producing errors

I am a rails novice and trying to create my web portfolio in Cloud9. I wanted to display my resume.pdf in the user browser so I installed this gem gem 'pdfjs_rails' now my app doesn't even start in cloud 9. It show the following error messages in the terminal:
Please help me. How can i revert it back? thanks
Doesn't look like anything related to pdfjs (no point in that direction). Are you sure you've selected the right runner in Cloud9? See:
https://community.c9.io/t/error-when-running-ruby-project/11799/4
More in general: if you think an issue would be related to a gem, you can simply remove it from the Gemfile and run gem bundle to 'revert' to a previous state.

Fixing this Error: NameError (uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column)

There are two apps. One of the apps is using rails 4.1.2. The other app is using rails 5.0.1. Here is how the rails 5 app works: it checks if the user is logged in:
If the user is logged in: then the user should be able to proceed forward.
If the user is NOT logged in, then it redirects them to the rails 4 app (which is where the user can log in). Once the user is logged in with the rails 4 app: the user should be able to access the rails 5 app no problem.
I am currently using the following gems in my Rails 5 project:
gem 'mysql2', '~> 0.4.4'
gem 'activerecord-session_store', '~> 1.0'
I then have the following in my config/initializers/session_store.rb file:
Rails.application.config.session_store :active_record_store, :key => ‘SOME_KEY’
Currently: if the user is not logged in: then it successfully redirects the user. The issue is when the user is logged in. I get this error when the logged in user attempts to access the rails 5 app:
NameError (uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::Column)
I do notice that the app is successfully connecting to the database
The app also appears to access the sessions table on initial request (when the user is not logged in).
Any suggestions on what is triggering this error and how to fix it? Ultimately: it almost appears as though rails 5 is incompatible with activerecord-session_store and mysql2.
We were eventually able to figure it out.
The Marshal module can serialize ruby objects to the database. It can also deserialize stuff from the database in order to recreate the objects.
When you serialize an object, there appears to be some low level info that goes in there too, like the mysql2 connection adapter.
When the Rails 5 app tries to deserialize the data, it throws this error because the constant that exists in the MySql2 connection adapter for Rails 4 does not exist in the Rails 5 version of that adapter.
Our work around was to just not store or retrieve any of the serialized objects from the sessions table for our rails 5 app. That did the trick.
If we had really needed to retrieve serialized objects from the sessions table for our rails 5 app: then I think we would have had to come up with a custom solution.
Hope this helps others in the future!
It may be due to changes in release i.e. Rails 5.0.
As In Ruby on Rails 5.0 Release Notes:
Removed support for the legacy mysql database adapter from core. Most users should be able to use mysql2. It will be converted to a separate gem when we find someone to maintain it.
Deprecated passing arguments to #tables - the #tables method of some adapters (mysql2, sqlite3) would return both tables and views while others (postgresql) just return tables. To make their behavior consistent, #tables will return only tables in the future.
Difference in constant as you described in your answer may be due to they going to change gem for mysql2 adapter.

unable to use ahoy gem for usage tracking

I'm trying to use ahoy_matey gem in development env. i.e. Windows7 64 bit and rails 4.0.9 by following the doc at https://github.com/ankane/ahoy.
I would like to use this gem to track user visits and ip addresses in particular and then restrict the number of sign-in's per IP address. I've followed the readme file but unable to see records in visits table in my pg db.
Below are the steps I've followed so far -
gem 'ahoy_matey' #included in gem file and ran bundle install
//= require ahoy # included this line in application.js file
rails generate ahoy:stores:active_record -d postgresql
rake db:migrate
gem 'fluent-logger' #ran bundle install after including this line in gem file
The below command generated an error- Could not find generator ahoy:stores:fluentd.
rails generate ahoy:stores:fluentd
I've tried including below script in /layout/application.html.erb and other pages as well inside script tags without success.
ahoy.trackAll();
included the below in my sign in method in session controller
ahoy.authenticate(user)
Included the below in my index method of landing page without success.
ahoy.track "Viewed Landing", title: "Landing page viewed"
The only thing that has worked so far is below code which I've added in my application controller and it is populating my ahoy_events table.
class ApplicationController < ActionController::Base
after_filter :track_action
def track_action
ahoy.track "Processed #{controller_name}##{action_name}", request.filtered_parameters
end
I've also restarted my rails webserver (I'm using thin in dev and puma in prod).
Could you please let me know what I'm missing here. What do I need to do differently with my ahoy setup to enable it to start writing records to the "visits table" in my pg database.
Any pointers will be appreciated.
TIA
have you try to find some js code that may not be compliant with browsers that doesn't appear in visits table ?
edit :
I detail my idea : if you have any js code not compliant that make browser didn't get 200 http success and then the page loading fail, if your ahoy call is after that, browser didn't execute it

Resources