I've followed these steps and I successfully see traffic going to my site, the problem is I can see the traffic of every controller/method EXCEPT those within the admin namespace, because the script at app/views/layouts/application.html.erb (the one shown below) is not executed at any ActiveAdmin page...
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXX-XX', 'herokuapp.com');
ga('send', 'pageview');
</script>
I know Devise is able to tell me how many times an user have sign in my site, but I would like to see more statistics....
How could I solve this? I don't want put a render partial with that code in every ActiveAdmin's controller/method just for this task... is there an app/views/layouts/application.html.erb equivalent for ActiveAdmin? I mean a way to put a script tag in every ActiveAdmin's view?
You can add custom scripts to ActiveAdmin's layout by simply editing app/assets/javascripts/active_admin.js. This file should have been created when you first set up ActiveAdmin.
Create a new javascript file to hold the Google Analytics script, and simply include the new script in active_admin.js.
It should look something like this:
// app/assets/javascripts/google_analytics.js
// Google analytics script goes here.
// app/assets/javascripts/active_admin.js
//= require active_admin/base
//= require ./google_analytics
Related
I'm trying to set up my app to have autocomplete options in text fields for my forms. So far, I've been able to successfully seed db and index through the Algolia dashboard. I can see my records are syncing correctly, so I know everything is good on the backend.
I'm now trying to implement the easiest possible way to get autocomplete on the homepage of my app. The page itself is a simple, static page with a basic page controller.
I've followed the setup instructions for autocomplete on github but haven't been able to get it to work. Here's what I currently have set up.
home.html.erb
<div id="autocomplete" class="border"></div>
<script src="https://cdn.jsdelivr.net/npm/#algolia/autocomplete-js"></script>
<script>
const { autocomplete } = window['#algolia/autocomplete-js'];
</script>
As far as I'm aware, this should work with no issue. But nothing displays, except the border class. I also tried the yarn install method but it also didn't seem to work.
In application.js,
import "#hotwired/turbo-rails"
import "./controllers"
import { autocomplete } from '#algolia/autocomplete-js';
const autocomplete = autocomplete({
container: '#autocomplete',
// ...
});
//= require jquery
//= require jquery_ujs
But when I keep that in there, as well as the can script on my home page, I get the error: Uncaught SyntaxError: Identifier 'autocomplete' has already been declared so as far as I can tell I've implemented it all correctly. I'm completely stuck here - if anyone has any insight, please let me know!
Is it possible to use Chartkick without rails? I would like to write a ruby script that generates a HTML newsletter which can be send by mail. The output should be a HTML file.
How do I integrate Chartkick into my project and supply it with the data needed?
Yes you can use chartkick without rails i'll show you basic example from there you can go to this link for more http://www.stuartellis.name/articles/erb/
inside irb type this
require 'chartkick'
include Chartkick::Helper
#data = [
["Washington", "1789-04-29", "1797-03-03"],
["Adams", "1797-03-03", "1801-03-03"],
["Jefferson", "1801-03-03", "1809-03-03"]
]
template = "<%= timeline #data%>"
renderer = ERB.new(template)
puts renderer.result()
This gives you html and js you need, but you have to include js manually
<script src="https://www.google.com/jsapi"></script>
<script src="chartkick.js"></script>
which you can download here chartkick
on a side note:
you dont even need to use ruby it's just javascript library chartkick.js
I would like to know where I need to place jQuery code written for specific controller action. For example I have two controllers Pets and Treats. Only the Index page for Treats controller needs to have an alert. Currently I have placed the jquery code in application.js and all my pages are pulling the alert.
If you do not want some script included by default, (in your own case, the script for index page for Treats), do the following:
Create a new file inside your javascript assets folder: app/assets/javascript/treats.js
put your script code inside here
Go into your application.js file and remove the //= require_tree . line.
NOTE that this will prevent any/all of your other js files from being loaded, so you will have to require all the other needed scripts individually.
Go into the view page where you want this one script called, (in your case, the Treats index.html.erb) and use a script tag to call this specific file (more accurately, the specific function inside the file).
By doing so, the script will not be available in your whole application, but will only be available where you want it, which is where you called it.
I'm looking into RequireJS but I'm uncertain about some things.
I understand how I can load all my dependencies with main.js.
But do I need to add any logic to work with those dependencies in main.js?
To me, main.js seems like a document.ready state and you enter logic there when the document has loaded, right?
And for other pages and partial views, do I need to create multiple main.js or can I just reference loaded functions in dependencies from the views in a <script> for example?
Update -
I've added an example of using RequireJS with modular HTML components. Build tool example included - https://github.com/simonsmith/modular-html-requirejs
I have also written a blog article about this - http://simonsmith.io/modular-html-components-with-requirejs/
The approach of just using main.js for everything is probably more suited to a single page application.
The way I've handled this situation is to only include common site-wide JS in the main.js file:
On every page:
<script src="require.js" data-main="main"></script>
main.js
require.config({
// config options
});
require(['jquery', 'common/ajaxLoader', 'common/someOtherModule'], function($, ajax, otherModule) {
// Modules that do stuff on every page are instantiated here
});
page1.html
<script>
require(['scripts/page1']);
</script>
page1.js
require(['jquery', 'page1Module'], function($, module){
// page1 specific stuff here
});
The above example is just one of a couple of ways to handle it. Note the difference between loading a plain JavaScript file and a module.
A rule of thumb I follow is to keep all reusable modules (or Classes if it makes it easier to conceptualise) inside a define with their own dependencies etc and then use require to grab those modules, use their methods or interact with them in some way.
Using this pattern will almost certainly require use of the domReady module that is a separate plugin for RequireJS. Use this instead of a ready function in jQuery for example, as it allows modules to begin downloading before the DOM is ready which reduces the wait for your code to execute.
Edit You may wish to see another example of multi-page application in the RequireJS repo
I have recently gone through the exercise of setting up RequrieJS with automatic build optimization in an ASP.NET MVC application. There are a lot of helpful blog articles such as Simon's that are a great reference. From an ASP.NET perspective one of the most useful I found in terms of configuring the RequireJS optimizer for multi-page ASP.NET applications was Making RequireJS play nice with ASP.NET MVC.
Using the great information already out there I have put up my own ASP.NET MVC RequireJS example on GitHub. Much of what is included is similar to examples already out there, however to address the issue of partial views, and multi-page require dependencies I have taken a slightly different approach.
_Layout.cshtml
The most notable difference from existing examples is the creation of a custom RequireViewPage that exposes methods to pass configuration data to RequrieJS as well as reference page specific require dependencies.
So your _Layout.cshtml will look much like what you'd expect with:
<head>
...
#RenderModuleConfig()
<script type="text/javascript" src="#Url.Script("vendor/require.js")" data-main="main"></script>
</head>
<body>
...
Views & Partials
To wire up views (and in my case knockout view models), an additional script fragment has been added to the bottom of _Layout.cshtml as follows
...
#RenderSection("scripts", required: false)
<script type="text/javascript">require(['main'], function () { require(['lib/knockout/knockout.require']); });</script>
</body>
This will ensure that for any view dependency, the main module has been loaded (assuming dependencies for main have being defined in main.js and then allows for view specific dependencies to be wired up via data attributes.
<div data-require="#MainModule"> ... </div>
<div data-require="#Module("address")"> ... </div>
<div data-require="view\home\index\model"> ... </div>
For a full explaination of the design and choices, see the README on GitHub
I have this index.html and login.html and I use a href to link from index to login. and in each index.html and login.html I import the javascript. However, it seems that only the ones come from index.html that is being loaded. so if I place the js in index.html for the login.html, it works fine. but then, we I place it separately ( another js for login.html that is not in index.html) , it doesnt work
TIA
When JQM(jQuery mobile) loads a page it uses ajax to accomplish this. When this happens all code in the <head> section is ignored. JQM looks for the data-role="page" part and inserts it into the same dom as index.html. So basically you are doing it the correct way when you add your js in the index.html page.
If you would like to compartmentalize your js code to work for certain pages use this example:
$(document).on('pageinit', '#page1', function(){
// code for #page1
});
$(document).on('pageinit', '#page2', function(){
// code for #page2
});
$(document).on('pageinit', '[data-role=page]', function(){
// this code will execute for every page that is data-role="page"
});
So go ahead and put all your code in one file. Split your code into appropriate pages like above and include that in your index.html file.
Also if you are using JQM version 1.0.1 with jQuery version 1.6.4(recommended with 1.0.1) use .delegate() instead of .on(). i.e.
$(document).delegate('#page1', 'pageinit', function(){ // notice that pageinit and #page1 are switched around for delegate
// code for #page1
}); // interesting to note that if you use delegate in jQuery 1.7.x it actually just calls the .on() method.
Note If you were making a web application instead of a phonegap app you would be smart to put your javascript in that one file and include that in every page. This way if someone is following a link or bookmarked your page they will still get the correct javascript file they need.
Anyways I hope that helps you out. Good luck!
If you are doing a window.location.href then it will load the new HTML(In you case it is login.html) If you are using this approach then you have to reload all you scripts again and hence add these scripts in all your .html pages.
<script src="cordova-1.6.0.js" type="text/javascript"></script>
<script type="text/javascript" src="jquery/jquery-1.7.1.min.js"></script>
<link rel="stylesheet" href="jquerymobile/jquery.mobile-1.1.0.min.css" />
<script type="text/javascript" src="jquerymobile/jquery.mobile-1.1.0.min.js"></script>
However if you use the approach recommended by #deadlock then you will just need to load the script once. The later approach is the best one.
Please post your code, specifically how the pages link to each other, and the global config settings you set for app, if any. Like much with jQM, there are multiple practices and strategies supported for page arch.
You can also learn by using desktop browser tools and view the "Resources" to see what and when your resources are being loaded.