bragboy
bragboy

Reputation: 35542

Active Admin - How to add custom script before </body> tag?

I would like to do some analytics on my active admin enabled rails application. For that, I need to paste some <script> and <noscript> code just before the </body> tag in my layout file. Unfortunately, I am not able to do that as the application.html layout file seems ineffective since ActiveAdmin renders its own layout files.

Is there a hook/place where I can insert the custom html code ?

Upvotes: 5

Views: 4391

Answers (3)

Abdullah Aden
Abdullah Aden

Reputation: 932

This is what worked for me. I found it on the activeadmin site (https://activeadmin.info/10-custom-pages.html).

# app/admin/calendar.rb
ActiveAdmin.register_page "Calendar" do
  index do
    render partial: 'calendar'
  end
end

# app/views/admin/calendar/_calendar.html.erb
<h1>Hello</h1>
<script>
</script>

Upvotes: 0

tfischbach
tfischbach

Reputation: 3051

Tested with ActiveAdmin 0.6.0 and Rails 4.0.5.

You can also override the arbre view used by active admin to render the footer. In your active_admin initializer add:

# config/initializers/active_admin.rb
require 'admin/analytics_footer'

ActiveAdmin.setup do |config|
  config.namespace :admin do |admin|
    config.view_factory.footer = Admin::AnalyticsFooter
  end
end

And define the view:

# lib/admin/analytics_footer.rb
module Admin
  class AnalyticsFooter < ActiveAdmin::Views::Footer
    def build
      super
      render('layouts/analytics')
    end
   end
 end

And place your ga tracking code in app/views/layouts/_analytics.html.erb. After a server restart the snippet should appear inside the footer at the end of the page.

Upvotes: 6

gmaliar
gmaliar

Reputation: 5479

Alright, first thing you'll have to clone the repository into your rails app, usually you'd put it in the vendor directory but rails throws this annoying warning that it will deprecate the use of the vendor directory style dir, it doesn't matter much if you're going to production with a 3.* version, so just do this into either vendor or lib directory in your rails app.

git clone git://github.com/gregbell/active_admin.git

Now change your Gemfile and have the gem loaded from the directory you set

gem 'activeadmin', :path => 'lib/activeadmin'

Now you have your own version of activeadmin, so whatever you need to edit you can do it directly from that dir, including changing the default layout that it is bundled with.

Few words of advise:

Although this method allows you to far more customize active admin you are fully aware that to update it to a newer version would need you to do some git pulling and merging if necessary.

I used this method with jquery-ui-rails plugin and with another gem, it works splendid and moreover you can contribute to the gem back if you add the hook you wanted to the gem itself. Good luck!

---- Edit ----

As you pointed out in a comment activeadmin doesn't work exactly how you would expect but no worries it's still an easy fix. Under the hood activeadmin uses something called arbre which is created and maintained by the same developer.

https://github.com/gregbell/arbre, it's just a DOM library for ruby.

So what you have to do is this:

Head over to this file inside the activeadmin dir you just cloned lib/active_admin/views/footer.rb

this is the footer of the activeadmin application, as you can see inside the build method you can insert inside something such as add_scripts method and below add

def add_scripts
  script :src => 'http://yoursource.com'
end

I am not fully sure how the arbre syntax flows but it shouldn't be hard to figure out.

Good luck!

Upvotes: 2

Related Questions