google_analytics_mailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google_analytics_mailer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Fabio Napoleoni
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,87 @@
1
+ # GoogleAnalyticsMailer
2
+
3
+ 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](http://support.google.com/analytics/bin/answer.py?hl=en&answer=1033867) to your URLs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'google_analytics_mailer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install google_analytics_mailer
18
+
19
+ ## Usage
20
+
21
+ There are three level of customization for rewritten links:
22
+
23
+ * **Class level:** Params specified at class level will appear on every link of every message generated by that class
24
+ * **Method level:** Params specified at method level will appear only on that message
25
+ * **View level:** Views level parameters will take the highest precedence and they will always override all the others
26
+
27
+ In order to enable Google Analytics params for a given mailer you should simply add a line to a given mailer as in:
28
+
29
+ ```ruby
30
+ class UserMailer < ActionMailer::Base
31
+ default :from => 'no-reply@example.com'
32
+
33
+ # declare url parameters for this mailer
34
+ google_analytics_mailer utm_source: 'newsletter', utm_medium: 'email' # etc
35
+
36
+ # Links in this email will have all links with GA params automatically inserted
37
+ def welcome
38
+ mail(to: 'user@example.com')
39
+ end
40
+ end
41
+ ```
42
+
43
+ Then in your view generate links as usual:
44
+
45
+ ```erb
46
+ <!-- this will produce your-url?utm_medium=email&utm_source=newsletter because of class default params -->
47
+ <%= link_to('Read online', newsletter_url) -%>
48
+ <!-- local parameters are not overridden, so this produces ?utm_medium=email&utm_source=my_newsletter -->
49
+ <%= link_to('Read online', newsletter_url(utm_source: 'my_newsletter')) -%>
50
+ ```
51
+
52
+ In order to override params for a specific message you can override params in the method which defines
53
+ the message as in:
54
+
55
+ ```ruby
56
+ class UserMailer < ActionMailer::Base
57
+ default :from => 'no-reply@example.com'
58
+
59
+ # declare url parameters for this mailer
60
+ google_analytics_mailer utm_source: 'newsletter', utm_medium: 'email' # etc
61
+
62
+ # Links in this email will have utm_source equal to second_newsletter
63
+ def welcome
64
+ google_analytics_params(utm_source: 'second_newsletter', utm_term: 'welcome2')
65
+ mail(to: 'user@example.com')
66
+ end
67
+ end
68
+ ```
69
+
70
+ At view level you can override generated parameters using the `with_google_analytics_params` method
71
+
72
+ ```erb
73
+ <div class="footer">
74
+ <%- with_google_analytics_params(utm_term: 'footer') do -%>
75
+ <!-- this will override other params and produces ?utm_medium=email&utm_source=newsletter&utm_term=footer -->
76
+ <%= link_to('Read online', newsletter_url) -%>
77
+ <%- end -%>
78
+ </div>
79
+ ```
80
+
81
+ ## Contributing
82
+
83
+ 1. Fork it
84
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
85
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
86
+ 4. Push to the branch (`git push origin my-new-feature`)
87
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google_analytics_mailer/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "google_analytics_mailer"
8
+ gem.version = GoogleAnalyticsMailer::VERSION
9
+ gem.authors = ["Fabio Napoleoni"]
10
+ gem.email = ["f.napoleoni@gmail.com"]
11
+ gem.description = %q{This gem add google analytics campagin tags to every link in your action mailer}
12
+ gem.summary = %q{This gem provides automatic Google Analytics tagged links in ActionMailer generated emails}
13
+ gem.homepage = "https://github.com/fabn/google_analytics_mailer"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ # gem dependencies for runtime
21
+ gem.add_runtime_dependency "addressable", "~> 2.3.0"
22
+ gem.add_runtime_dependency "actionmailer", "~> 3.2.0"
23
+
24
+ # gem dependencies for development
25
+ gem.add_development_dependency "rake"
26
+ gem.add_development_dependency "rspec", "~> 2.12.0"
27
+ gem.add_development_dependency "email_spec", "~> 1.2.0"
28
+ end
@@ -0,0 +1,40 @@
1
+ require "addressable/uri"
2
+
3
+ module GoogleAnalyticsMailer # :nodoc:
4
+
5
+ # This module is added as helper to ActionMailer objects and override
6
+ # the url_for default implementation to insert GA params into generated links
7
+ module UrlFor
8
+
9
+ # Override default url_for method to insert ga params
10
+ #
11
+ # @return [String] the modified url
12
+ def url_for(original_url)
13
+ # Fetch final parameters calling private method
14
+ params_to_add = controller.computed_analytics_params.with_indifferent_access
15
+ # temporary override coming from with_google_analytics_params method
16
+ params_to_add.merge!(@_override_ga_params) if @_override_ga_params.try(:any?)
17
+ # if there are no parameters return super value
18
+ if params_to_add.empty?
19
+ super(original_url)
20
+ else
21
+ # else parse the url and append given parameters
22
+ ::Addressable::URI.parse(super(original_url)).tap do |url|
23
+ url.query_values = (url.query_values || {}).reverse_merge(params_to_add) if url.absolute?
24
+ end.to_s.html_safe
25
+ end
26
+ end
27
+
28
+ # Allow to override Google Analytics params for a given block in views
29
+ # @return [String]
30
+ def with_google_analytics_params(params)
31
+ raise ArgumentError, "Missing block" unless block_given?
32
+ @_override_ga_params = params
33
+ yield
34
+ @_override_ga_params = nil
35
+ nil # do not return any value
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,3 @@
1
+ module GoogleAnalyticsMailer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,56 @@
1
+ require "google_analytics_mailer/version"
2
+ require "google_analytics_mailer/url_for"
3
+ require "action_mailer"
4
+ require "active_support/concern"
5
+
6
+ # This module declares the main class method which is then callable from every
7
+ # ActionMailer class
8
+ module GoogleAnalyticsMailer
9
+
10
+ # These are the currently GA allowed get params for link tagging
11
+ VALID_ANALYTICS_PARAMS = [:utm_source, :utm_medium, :utm_campaign,
12
+ :utm_term, :utm_content]
13
+
14
+ # Enable google analytics link tagging for the mailer which call this method
15
+ def google_analytics_mailer params = {}
16
+ if (params.keys - VALID_ANALYTICS_PARAMS).any?
17
+ raise ArgumentError, "Invalid parameters keys #{params.keys - VALID_ANALYTICS_PARAMS}"
18
+ end
19
+
20
+ # add accessor for class level parameters
21
+ cattr_accessor(:google_analytics_class_params) { params }
22
+
23
+ # include the module which provides the actual functionality
24
+ include GoogleAnalytics
25
+
26
+ end
27
+
28
+ # This module provides methods to deal with parameter merging and similar stuff
29
+ module GoogleAnalytics
30
+
31
+ # uses concern to include it
32
+ extend ActiveSupport::Concern
33
+
34
+ # this code is evaluated in class context
35
+ included do
36
+ helper GoogleAnalyticsMailer::UrlFor
37
+ end
38
+
39
+ # This method return the actual parameters to use when building links
40
+ # @return [Hash] computed parameters
41
+ def computed_analytics_params
42
+ @_computed_ga_params ||= self.class.google_analytics_class_params.merge(@_ga_instance_params || {})
43
+ end
44
+
45
+ private
46
+ # Instance level parameters, used only for the given message
47
+ def google_analytics_params params
48
+ @_ga_instance_params = params
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+
55
+ # Add the class method to ActionMailer::Base
56
+ ActionMailer::Base.send :extend, GoogleAnalyticsMailer
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleAnalyticsMailer do
4
+
5
+ it "ActionMailer::Base should extend GoogleAnalyticsMailer" do
6
+ (class << ActionMailer::Base; self end).included_modules.should include(GoogleAnalyticsMailer)
7
+ end
8
+
9
+ describe ".google_analytics_mailer" do
10
+
11
+ class TestMailer1 < ActionMailer::Base
12
+ end
13
+
14
+ it "should raise on invalid options for GA params" do
15
+ expect {
16
+ ActionMailer::Base.google_analytics_mailer(foo: 'bar')
17
+ }.to raise_error(ArgumentError, /:foo/)
18
+ end
19
+
20
+ it "should assign given parameters to a class variable" do
21
+ params = {utm_source: 'newsletter', utm_medium: 'email'}
22
+ TestMailer1.google_analytics_mailer(params)
23
+ TestMailer1.google_analytics_class_params.should == params
24
+ end
25
+
26
+ end
27
+
28
+ class UserMailer < ActionMailer::Base
29
+ default :from => 'no-reply@example.com'
30
+
31
+ # declare url parameters for this mailer
32
+ google_analytics_mailer utm_source: 'newsletter', utm_medium: 'email' # etc
33
+
34
+ # simulate url helper
35
+ helper do
36
+ def newsletter_url params = {}
37
+ 'http://www.example.com/newsletter'.tap do |u|
38
+ u << "?#{params.to_param}" if params.any?
39
+ end.html_safe
40
+ end
41
+ end
42
+
43
+ # Links in this email will have all links with GA params automatically inserted
44
+ def welcome
45
+ mail(to: 'user@example.com')
46
+ end
47
+
48
+ def welcome2
49
+ google_analytics_params(utm_source: 'second_newsletter', utm_term: 'welcome2')
50
+ mail(to: 'user@example.com')
51
+ end
52
+
53
+ def welcome3
54
+ mail(to: 'user@example.com')
55
+ end
56
+
57
+ end
58
+
59
+ describe UserMailer do
60
+
61
+ # see view in spec/support/views/user_mailer/welcome.html.erb
62
+ describe "#welcome" do
63
+
64
+ subject { UserMailer.welcome }
65
+
66
+ it "should have analytics link with params taken from class definition" do
67
+ subject.should have_body_text 'http://www.example.com/newsletter?utm_medium=email&utm_source=newsletter'
68
+ end
69
+
70
+ it "should have analytics link with overridden params" do
71
+ subject.should have_body_text 'http://www.example.com/newsletter?utm_medium=email&utm_source=my_newsletter'
72
+ end
73
+
74
+ end
75
+
76
+ # see view in spec/support/views/user_mailer/welcome2.html.erb
77
+ describe "#welcome2" do
78
+
79
+ subject { UserMailer.welcome2 }
80
+
81
+ it "should have analytics link with params taken from instance" do
82
+ subject.should have_body_text 'http://www.example.com/newsletter?utm_medium=email&utm_source=second_newsletter&utm_term=welcome2'
83
+ end
84
+
85
+ it "should have analytics link with overridden params" do
86
+ subject.should have_body_text 'http://www.example.com/newsletter?utm_medium=email&utm_source=my_newsletter&utm_term=welcome2'
87
+ end
88
+
89
+ end
90
+
91
+ # see view in spec/support/views/user_mailer/welcome3.html.erb
92
+ describe "#welcome3" do
93
+
94
+ subject { UserMailer.welcome3 }
95
+
96
+ it "should have analytics link with params taken from view" do
97
+ subject.should have_body_text 'http://www.example.com/newsletter?utm_medium=email&utm_source=newsletter&utm_term=footer'
98
+ end
99
+
100
+ end
101
+
102
+ end
103
+
104
+ end
@@ -0,0 +1,23 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+
4
+ # Gem main file
5
+ require 'google_analytics_mailer'
6
+ require 'active_support/all'
7
+ require 'email_spec'
8
+
9
+ # Requires supporting ruby files with custom matchers and macros, etc,
10
+ # in spec/support/ and its subdirectories.
11
+ Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f }
12
+
13
+ # Configure action mailer for test deliveries
14
+ ActionMailer::Base.delivery_method = :test
15
+ # Configure action mailer view path
16
+ ActionMailer::Base.view_paths = File.join(File.dirname(__FILE__), 'support', 'views')
17
+
18
+ RSpec.configure do |config|
19
+
20
+ config.include EmailSpec::Helpers
21
+ config.include EmailSpec::Matchers
22
+
23
+ end
@@ -0,0 +1,7 @@
1
+ <h1>Newsletter title</h1>
2
+ <div class="body">
3
+ <!-- this will produce ?utm_medium=email&utm_source=newsletter because of class default params -->
4
+ <%= link_to('Read online', newsletter_url) -%>
5
+ <!-- local parameters are not overridden, so this produces ?utm_medium=email&utm_source=my_newsletter -->
6
+ <%= link_to('Read online', newsletter_url(utm_source: 'my_newsletter')) -%>
7
+ </div>
@@ -0,0 +1,7 @@
1
+ <h1>Newsletter title</h1>
2
+ <div class="body">
3
+ <!-- this will produce ?utm_medium=email&utm_source=second_newsletter because of instance params -->
4
+ <%= link_to('Read online', newsletter_url) -%>
5
+ <!-- local parameters are not overridden, so this produces ?utm_medium=email&utm_source=my_newsletter -->
6
+ <%= link_to('Read online', newsletter_url(utm_source: 'my_newsletter')) -%>
7
+ </div>
@@ -0,0 +1,12 @@
1
+ <h1>Newsletter title</h1>
2
+ <div class="body">
3
+ <p>Lorem ipsum</p>
4
+ </div>
5
+ <div class="footer">
6
+ <%- with_google_analytics_params(utm_term: 'footer') do -%>
7
+ <!-- this will override params and produces ?utm_medium=email&utm_source=newsletter&utm_term=footer -->
8
+ <%= link_to('Read online', newsletter_url) -%>
9
+ <%- end -%>
10
+ </div>
11
+
12
+
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_analytics_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Fabio Napoleoni
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: addressable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.3.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: actionmailer
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 3.2.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.12.0
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.12.0
78
+ - !ruby/object:Gem::Dependency
79
+ name: email_spec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 1.2.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 1.2.0
94
+ description: This gem add google analytics campagin tags to every link in your action
95
+ mailer
96
+ email:
97
+ - f.napoleoni@gmail.com
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files: []
101
+ files:
102
+ - .gitignore
103
+ - .rspec
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - google_analytics_mailer.gemspec
109
+ - lib/google_analytics_mailer.rb
110
+ - lib/google_analytics_mailer/url_for.rb
111
+ - lib/google_analytics_mailer/version.rb
112
+ - spec/lib/google_analytics_mailer_spec.rb
113
+ - spec/spec_helper.rb
114
+ - spec/support/views/user_mailer/welcome.html.erb
115
+ - spec/support/views/user_mailer/welcome2.html.erb
116
+ - spec/support/views/user_mailer/welcome3.html.erb
117
+ homepage: https://github.com/fabn/google_analytics_mailer
118
+ licenses: []
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ segments:
130
+ - 0
131
+ hash: -1090596754510685876
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ segments:
139
+ - 0
140
+ hash: -1090596754510685876
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 1.8.24
144
+ signing_key:
145
+ specification_version: 3
146
+ summary: This gem provides automatic Google Analytics tagged links in ActionMailer
147
+ generated emails
148
+ test_files:
149
+ - spec/lib/google_analytics_mailer_spec.rb
150
+ - spec/spec_helper.rb
151
+ - spec/support/views/user_mailer/welcome.html.erb
152
+ - spec/support/views/user_mailer/welcome2.html.erb
153
+ - spec/support/views/user_mailer/welcome3.html.erb