Skip to content

Commit

Permalink
resolves danielbayerlein#32 add support for gtag variant of tracking …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
mojavelinux committed Feb 6, 2018
1 parent 73277f1 commit d7d077f
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
18 changes: 17 additions & 1 deletion features/google_analytics.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Google Analytics tag helper

Scenario: Basic tracking code
Scenario: Analytics tracking code
Given the Server is running at "basic-app"
When I go to "/google-analytics.html"
Then I should see:
Expand All @@ -15,6 +15,22 @@ Feature: Google Analytics tag helper
</script>
"""

Scenario: gtag tracking code
Given the Server is running at "gtag-app"
When I go to "/google-analytics.html"
Then I should see:
"""
<script async src="//www.googletagmanager.com/gtag/js?id=UA-123456-78"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag () {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-123456-78');
</script>
"""

Scenario: Tracking across a domain and its subdomains
Given the Server is running at "across-a-domain-app"
When I go to "/google-analytics.html"
Expand Down
4 changes: 4 additions & 0 deletions fixtures/gtag-app/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activate :google_analytics do |ga|
ga.tracking_id = 'UA-123456-78'
ga.variant = 'gtag'
end
1 change: 1 addition & 0 deletions fixtures/gtag-app/source/google-analytics.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= google_analytics_tag %>
6 changes: 6 additions & 0 deletions lib/middleman-google-analytics/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class GoogleAnalyticsExtension < Extension
option :test, false, 'Testing your implementation without sending hits'
option :minify, false, 'Compress the JavaScript code'
option :output, :html, 'Output style - :html includes <script> tag'
option :variant, :analytics, 'Tracking code variant'

def after_configuration
options.test = true if legacy_development?
Expand All @@ -28,6 +29,11 @@ def after_configuration
raise ArgumentError, 'No domain_name given' if app.build?
end

unless [:analytics, :gtag].include?(options.variant.try(:to_sym))
$stderr.puts 'Google Analytics: Please specify a valid variant type (analytics|gtag).'
raise ArgumentError, 'Only "analytics" or "gtag" allowed' if app.build?
end

unless [:html, :js].include?(options.output.try(:to_sym))
$stderr.puts 'Google Analytics: Please specify a valid output type (html|js).'
raise ArgumentError, 'Only "html" or "js" allowed' if app.build?
Expand Down
9 changes: 9 additions & 0 deletions lib/middleman-google-analytics/gtag.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script async src="//www.googletagmanager.com/gtag/js?id=<%= @options.tracking_id %>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag () {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', '<%= @options.tracking_id %>');
</script>
21 changes: 15 additions & 6 deletions lib/middleman-google-analytics/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@ def google_analytics_tag

return nil if options.disable

file = File.join(File.dirname(__FILE__), 'analytics.js.erb')
file = File.join(File.dirname(__FILE__), "#{options.variant}.js.erb")
context = { options: options }
content = Erubis::FastEruby.new(File.read(file)).evaluate(context)
content = Uglifier.compile(content) if options.minify

if options.output.to_sym == :html
content = indent(content) unless options.minify
content_tag(:script, content, type: 'text/javascript')
if options.variant.to_sym == :analytics
if options.output.to_sym == :html
content = options.minify ? Uglifier.compile(content) : indent(content)
content_tag(:script, content, type: 'text/javascript')
else
options.minify ? Uglifier.compile(content) : content
end
else
content
if options.output.to_sym == :html
content.gsub(/(?<=<script>)(.+)(?=<\/script>)/m) do
options.minify ? Uglifier.compile($1) : indent($1)
end
else
options.minify ? content.gsub(/.*<script>(.+)<\/script>/m) { Uglifier.compile($1) } : content
end
end
end

Expand Down

0 comments on commit d7d077f

Please sign in to comment.