From abc446c4ff7a65396a0736d6a0eed381b1b48156 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Tue, 6 Feb 2018 00:33:03 -0700 Subject: [PATCH] resolves #32 add support for gtag variant of tracking code --- features/google_analytics.feature | 18 ++++++++++++++- fixtures/gtag-app/config.rb | 4 ++++ .../gtag-app/source/google-analytics.html.erb | 1 + lib/middleman-google-analytics/extension.rb | 6 +++++ lib/middleman-google-analytics/gtag.js.erb | 9 ++++++++ lib/middleman-google-analytics/helpers.rb | 23 +++++++++++++------ 6 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 fixtures/gtag-app/config.rb create mode 100644 fixtures/gtag-app/source/google-analytics.html.erb create mode 100644 lib/middleman-google-analytics/gtag.js.erb diff --git a/features/google_analytics.feature b/features/google_analytics.feature index 1e6e46c..82e2391 100644 --- a/features/google_analytics.feature +++ b/features/google_analytics.feature @@ -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: @@ -15,6 +15,22 @@ Feature: Google Analytics tag helper """ + Scenario: gtag tracking code + Given the Server is running at "gtag-app" + When I go to "/google-analytics.html" + Then I should see: + """ + + + """ + 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" diff --git a/fixtures/gtag-app/config.rb b/fixtures/gtag-app/config.rb new file mode 100644 index 0000000..f71bfea --- /dev/null +++ b/fixtures/gtag-app/config.rb @@ -0,0 +1,4 @@ +activate :google_analytics do |ga| + ga.tracking_id = 'UA-123456-78' + ga.variant = 'gtag' +end diff --git a/fixtures/gtag-app/source/google-analytics.html.erb b/fixtures/gtag-app/source/google-analytics.html.erb new file mode 100644 index 0000000..22eaa6f --- /dev/null +++ b/fixtures/gtag-app/source/google-analytics.html.erb @@ -0,0 +1 @@ +<%= google_analytics_tag %> diff --git a/lib/middleman-google-analytics/extension.rb b/lib/middleman-google-analytics/extension.rb index 43b35c4..d51e419 100644 --- a/lib/middleman-google-analytics/extension.rb +++ b/lib/middleman-google-analytics/extension.rb @@ -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 + diff --git a/lib/middleman-google-analytics/helpers.rb b/lib/middleman-google-analytics/helpers.rb index 4577103..c4a2572 100644 --- a/lib/middleman-google-analytics/helpers.rb +++ b/lib/middleman-google-analytics/helpers.rb @@ -11,22 +11,31 @@ 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("\n" + 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(/(?<=