Module: Decidim::NewslettersHelper

Included in:
NewsletterMailer, Decidim::NewsletterTemplates::BaseCell, NewslettersController
Defined in:
app/helpers/decidim/newsletters_helper.rb

Overview

Helper that provides methods to render links with utm codes, and replaced name

Instance Method Summary collapse

Instance Method Details

#custom_url_for_mail_root(organization, newsletter_id = nil) ⇒ Object

this method is used to generate the root link on mail with the utm_codes If the newsletter_id is nil, it returns the root_url



33
34
35
36
37
38
39
# File 'app/helpers/decidim/newsletters_helper.rb', line 33

def custom_url_for_mail_root(organization, newsletter_id = nil)
  if newsletter_id.present?
    decidim.root_url(host: organization.host) + utm_codes(organization.host, newsletter_id.to_s)
  else
    decidim.root_url(host: organization.host)
  end
end

#parse_interpolations(content, user = nil, id = nil) ⇒ Object

If the newsletter body there are some links and the Decidim.track_newsletter_links = true it will be replaced with the utm_codes method described below. for example transform “es.lipsum.com/” to “es.lipsum.com/?utm_source=localhost&utm_campaign=newsletter_11” And replace “%name” on the subject or content of newsletter to the user Name for example transform “%name” to “User Name”



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/helpers/decidim/newsletters_helper.rb', line 11

def parse_interpolations(content, user = nil, id = nil)
  if Decidim.config.track_newsletter_links && id.present? && user.present?
    host = user.organization.host.to_s
    campaign = "newsletter_#{id}"

    links = content.scan(/href\s*=\s*"([^"]*)"/)

    links.each do |link|
      link_replaced = link.first + utm_codes(host, campaign)
      content = content.gsub(/href\s*=\s*"([^"]*#{link.first})"/, %(href="#{link_replaced}"))
    end
  end

  if user.present?
    content.gsub("%{name}", user.name)
  else
    content.gsub("%{name}", "")
  end
end

#utm_codes(host, newsletter_id) ⇒ Object

Method to specify the utm_codes. You can change or add utm_codes for track



43
44
45
# File 'app/helpers/decidim/newsletters_helper.rb', line 43

def utm_codes(host, newsletter_id)
  "?utm_source=#{host}&utm_campaign=#{newsletter_id}"
end