Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Gxapi interfaces with Google Analytics Experiments to retrieve data from its API and determine which variant should be presented to a given user
Config settings can be manually set instead of using a YML file.
# initializers/gxapi.rb
Gxapi.config {
google_analytics: {
account_id: ACCOUNT_ID,
profile_id: PROFILE_ID,
web_property_id: WEB_PROPERTY_ID,
},
google: {
email: SERVICE_ACCOUNT_EMAIL,
private_key_path: 'PATH_TO_SERVICE_ACCOUNT_PRIVATE_KEY'
}
}
Private key can be passed as Base64 string instead of being read from a file. Use the private_key
setting instead of private_key_path
in the config.
% gem install gxapi_rails
% rails g gxapi:install
First, you must create a Google Analytics Service Account. Information can be found here https://developers.google.com/accounts/docs/OAuth2ServiceAccount
Gxapi uses #{Rails.root}/config/gxapi.yml
to configure variables.
You can use different configurations per environment
(development/test/production)
Gxapi looks for a Service Account Private Key in config/google.p12
by default. It is best to not include the key in source control and
to either drop it off where necessary with a script or symlink it.
development:
google_analytics:
account_id: ACCOUNT_ID
profile_id: PROFILE_ID
web_property_id: WEB_PROPERTY_ID
google:
email: SERVICE_ACCOUNT_EMAIL
private_key_path: 'PATH_TO_SERVICE_ACCOUNT_PRIVATE_KEY'
These values can be specified in the installer if you have them ahead of time
% rails g gxapi:install --account-id=ACCOUNT_ID \
--profile-id=PROFILE_ID --web-property-id=WEB_PROPERTY_ID \
--email=EMAIL
Some instructions on creating a Google Analytics Service Account
Some instructions on where to find your Google Analytics settings
We load experiment data either
Once the data for the experiment is loaded, its variant weights (the percentage each variant is shown) will not change until the data is reloaded explicitly. This is so we do not need to make an API call to Google each time a page loads.
To refresh the experiment data, you can call a rake task either manually or at some interval on a schedular (such as cron)
% bundle exec rake gxapi:reload_experiments
# /etc/cron.d/gxapi
0 * * * * USER bundle exec rake gxapi:reload_experiments
First, add the experiment Javascript in your layout
# app/views/layouts/application.html.erb
<%= gxapi_experiment_js %>
This will render the Javascript tag to pull in the Google Analytics Content Experiment JS source and the corresponding call to send your experiment data in. If you are specifying a custom variable name (the default is 'variant'), you can add it here as well
# app/views/layouts/application.html.erb
<%= gxapi_experiment_js(:custom_var) %>
If you have multiple experiments on the same page, you can add this same call for each experiment in a view
# app/views/layouts/application.html.erb
<%= gxapi_experiment_js %>
# app/views/posts/index.html.erb
<%= gxapi_experiment_js(:other_experiment_key) %>
Once you have set up an experiment in Google Analytics, you can reference it by name or experiment_id in your controller
class PostsController < ApplicationController
def index
gxapi_get_variant("My Experiment")
end
# OR
def other_action
gxapi_get_variant(id: 'long_hex_experiment_id')
end
end
This will use the default key variant
for the experiment. To customize
this name, you can pass another value. This is useful when you are using
multiple experiments on the same page
class PostsController < ApplicationController
def index
gxapi_get_variant("My Experiment", :exp1)
gxapi_get_variant("Other Experiment", :exp2)
end
end
In the controller and the view, you can access the variant name to determine
which path your code should take. gxapi_get_variant
returns a
Celluloid::Future
to prevent blocking while the result is calculated. To
get the value in the controller you can call #value
. In the view,
Gxapi provides a the gxapi_variant_name
helper.
class PostsController < ApplicationController
def index
gxapi_get_variant("My Experiment", :exp1)
end
end
# app/views/posts/index.html.erb
<h1>My Page</h1>
<% if gxapi_variant_name(:exp1) == 'version_a' -%>
Do some stuff...
<% else -%>
Do some other stuff...
<% end -%>
In order to view a specific version, you can pass experiment_name=value
into a GET request to the action. This will short-circuit the rendering
of the JavaScript call to Google so testing will not affect your results
# GET /posts.html?variant=my_var
# app/controllers/posts_controller.rb
def index
gxapi_get_variant("My Experiment")
end
# app/views/posts/index.html.erb
gxapi_variant_name # Will always equal 'my_var'
# GET /posts.html?custom_name=my_var
# app/controllers/posts_controller.rb
def index
gxapi_get_variant("My Experiment", :custom_name)
end
# app/views/posts/index.html.erb
gxapi_variant_name(:custom_name) # Will always equal 'my_var'
Copyright (c) 2013 Dan Langevin. See LICENSE.txt for further details.
FAQs
Unknown package
We found that gxapi_rails demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.