🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

google_analytics_mailer

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

google_analytics_mailer

bundlerRubyGems.org
Version
0.3.0
Version published
Maintainers
1
Created
Source

GoogleAnalyticsMailer

Gem Version Build Status Coverage Status Code Climate Dependency Status

This gem automatically rewrites absolute links generated by ActionMailer. It intercepts all'url_for calls (so link_to calls are intercepted as well) and change the final url to add Custom Campaign parameters to your URLs.

Installation

Add this line to your application's Gemfile:

gem 'google_analytics_mailer'

And then execute:

$ bundle

Or install it yourself as:

$ gem install google_analytics_mailer

Usage

There are three level of customization for rewritten links:

  • Class level: Params specified at class level will appear on every link of every message generated by that class
  • Method level: Params specified at method level will appear only on that message
  • View level: Views level parameters will take the highest precedence and they will always override all the others

In order to enable Google Analytics params for a given mailer you should simply add a line to a given mailer as in:

class UserMailer < ActionMailer::Base
  default :from => 'no-reply@example.com'

  # declare url parameters for this mailer
  google_analytics_mailer utm_source: 'newsletter', utm_medium: 'email' # etc

  # Links in this email will have all links with GA params automatically inserted
  def welcome
    mail(to: 'user@example.com')
  end
end

Then in your view generate links as usual:

<!-- this will produce your-url?utm_medium=email&utm_source=newsletter because of class default params -->
<%= link_to('Read online', newsletter_url) -%>
<!-- local parameters are not overridden, so this produces ?utm_medium=email&utm_source=my_newsletter -->
<%= link_to('Read online', newsletter_url(utm_source: 'my_newsletter')) -%>

In order to override params for a specific message you can override params in the method which defines the message as in:

class UserMailer < ActionMailer::Base
  default :from => 'no-reply@example.com'

  # declare url parameters for this mailer
  google_analytics_mailer utm_source: 'newsletter', utm_medium: 'email' # etc

  # Links in this email will have utm_source equal to second_newsletter
  def welcome
    google_analytics_params(utm_source: 'second_newsletter', utm_term: 'welcome2')
    mail(to: 'user@example.com')
  end
end

At view level you can override generated parameters using the with_google_analytics_params method

<div class="footer">
  <%= with_google_analytics_params(utm_term: 'footer') do -%>
    <!-- this will override other params and produces ?utm_medium=email&utm_source=newsletter&utm_term=footer -->
    <%= link_to('Read online', newsletter_url) -%>
  <%- end -%>
</div>

or you can disable them for a specific block

<div class="footer">
  <%= without_google_analytics_params do -%>
    <!-- this will output link with no analytics params at all -->
    <%= link_to('Read online', newsletter_url) -%>
  <%- end -%>
</div>

Action Controller integration

Since ActionMailer and ActionController use the same code base to provide view rendering this gem can be used in the same way in any action controller which inherits from ActionController::Base (i.e. almost every controller in a Rails application). The alias google_analytics_controller is provided for better naming thus in a controller you can do

class UserController < ApplicationController

  # declare url parameters for this controller. Absolute links in this controller will be tagged with GA parameters
  google_analytics_controller utm_source: 'some site', utm_medium: 'web' # etc

  # Override for single action
  def index
    google_analytics_params(utm_source: 'index page', utm_term: 'foo bar')
  end
end

View syntax is obviously the same as in mailer.

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

FAQs

Package last updated on 19 Jun 2015

Did you know?

Socket

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.

Install

Related posts