Module: GoogleAnalyticsMailer::UrlFor

Defined in:
lib/google_analytics_mailer/url_for.rb

Overview

This module is added as helper to ActionMailer objects and override the url_for default implementation to insert GA params into generated links

Instance Method Summary collapse

Instance Method Details

#url_for(original_url) ⇒ String

Override default url_for method to insert ga params

Returns:

  • (String)

    the modified url



10
11
12
13
14
15
16
17
# File 'lib/google_analytics_mailer/url_for.rb', line 10

def url_for(original_url)
  # Fetch final parameters calling private method
  params_to_add = controller.computed_analytics_params.with_indifferent_access
  # temporary override coming from with_google_analytics_params method
  params_to_add.merge!(@_override_ga_params) if @_override_ga_params.try(:any?)
  # Avoid parse if params not given
  params_to_add.empty? ? super(original_url) : builder.build(super(original_url), params_to_add)
end

#with_google_analytics_params(params) { ... } ⇒ String

Acts as an around filter from the given block and return its content by merging the given analytics parameters to current ones

Examples:

in an ERB template

<%= with_google_analytics_params(utm_content: 'foo') do -%>
  <%= link_to 'foo', 'http://example.com' -%>
<%- end -%>

Inline usage now possible because of captured output

<%= with_google_analytics_params(utm_content: 'foo') { link_to ... } -%>

Parameters:

  • params (Hash)

    options for url to build

Options Hash (params):

  • :utm_campaign (String)

    required is the main GA param

  • :utm_content (String)

    content of the campaign

  • :utm_source (String)

    campaign source

  • :utm_medium (String)

    campaign medium

  • :utm_term (String)

    keyword for this campaign

Yields:

  • The given block is executed with overridden analytics parameters

Yield Returns:

  • (String)

    content returned from the block with filtered parameters

Returns:

  • (String)

    the output returned by block



41
42
43
44
45
46
47
# File 'lib/google_analytics_mailer/url_for.rb', line 41

def with_google_analytics_params(params, &block)
  raise ArgumentError, 'Missing block' unless block_given?
  @_override_ga_params = params
  capture(&block)
ensure
  @_override_ga_params = nil
end

#without_google_analytics_params { ... } ⇒ String

Acts as an around filter from the given block and return its content without inserting any analytics tag

Examples:

in an ERB template

<%= without_google_analytics_params(utm_content: 'foo') do -%>
  <%= link_to 'foo', 'http://example.com' -%>
<%- end -%>

Inline usage now possible because of captured output

<%= without_google_analytics_params(utm_content: 'foo') { link_to ... } -%>

Yields:

  • The given block is executed with appeding analytics parameters

Yield Returns:

  • (String)

    content returned from the block with no parameters appended

Returns:

  • (String)

    the output of the block executed without analytics params



64
65
66
67
68
69
70
# File 'lib/google_analytics_mailer/url_for.rb', line 64

def without_google_analytics_params(&block)
  raise ArgumentError, 'Missing block' unless block_given?
  @_override_ga_params = Hash[VALID_ANALYTICS_PARAMS.zip([nil])]
  capture(&block)
ensure
  @_override_ga_params = nil
end