Sha256: 2e648441148979aea44af770d7967c1c4107da20d1974723fcfc97c1e5b045ac

Contents?: true

Size: 1.27 KB

Versions: 13

Compression:

Stored size: 1.27 KB

Contents

module Locomotive
  module Steam
    module Liquid
      module Tags
        class GoogleAnalytics < ::Liquid::Tag

          Syntax = /(#{::Liquid::QuotedFragment}+)/o.freeze

          attr_reader :account_id

          def initialize(tag_name, markup, options)
            super

            if markup =~ Syntax
              @account_id = ::Liquid::Expression.parse($1)
            else
              raise ::Liquid::SyntaxError.new("Syntax Error in 'google_analytics' - Valid syntax: google_analytics <account_id>")
            end
          end

          def render(context)
            ga_snippet(context.evaluate(account_id))
          end

          private

          def ga_snippet(account_id)
            %{
              <!-- Global Site Tag (gtag.js) - Google Analytics -->
              <script async src="https://www.googletagmanager.com/gtag/js?id=#{account_id}"></script>
              <script>
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());

                gtag('config', '#{account_id}');
              </script>
            }
          end

        end

        ::Liquid::Template.register_tag('google_analytics'.freeze, GoogleAnalytics)

      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
locomotivecms_steam-1.8.0.alpha2 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.8.0.alpha1 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.7.1 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.7.0 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.6.1 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.6.0 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.6.0.rc1 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.6.0.beta1 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.5.3 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.5.2 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.5.1 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.5.0 lib/locomotive/steam/liquid/tags/google_analytics.rb
locomotivecms_steam-1.5.0.rc1 lib/locomotive/steam/liquid/tags/google_analytics.rb