Sha256: 84722e712932d29c18f87bc0e2f8bfba0fbf13fc60f23e7ae5d6f99d23497811

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require 'uglifier'

module Middleman
  class GoogleAnalyticsExtension < Extension
    option :tracking_id, nil, 'Property ID'
    option :anonymize_ip, false, 'Removing the last octet of the IP address'
    option :domain_name, nil, 'Tracking across a domain and its subdomains'
    option :allow_linker, false, 'Tracking across multiple domains and ' \
                                 'subdomains'
    option :debug, false, 'Tracking Code Debugger'
    option :development, true, 'Tracking in development environment'
    option :minify, false, 'Compress the JavaScript code'

    def initialize(app, options_hash={}, &block)
      super

      app.set :google_analytics_settings, options
    end

    def after_configuration
      unless options.tracking_id
        $stderr.puts 'Google Analytics: Please specify a property ID'
        raise 'No property ID given'
      end

      if options.allow_linker and not options.domain_name
        $stderr.puts 'Google Analytics: Please specify a domain_name when ' \
                     'using allow_linker'
        raise 'No domain_name given'
      end
    end

    helpers do
      def google_analytics_tag
        @options = google_analytics_settings
        file = File.join(File.dirname(__FILE__), 'ga.js.erb')
        content = ERB.new(File.read(file)).result(binding)
        content = Uglifier.compile(content) if google_analytics_settings.minify
        content_tag(:script, content, type: 'text/javascript')
      end

      def google_analytics_universal_tag
        @options = google_analytics_settings
        file = File.join(File.dirname(__FILE__), 'analytics.js.erb')
        content = ERB.new(File.read(file)).result(binding)
        content = Uglifier.compile(content) if google_analytics_settings.minify
        content_tag(:script, content, type: 'text/javascript')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
middleman-google-analytics-1.0.2 lib/middleman-google-analytics/extension.rb
middleman-google-analytics-1.0.0 lib/middleman-google-analytics/extension.rb
middleman-google-analytics-1.0.0.pre lib/middleman-google-analytics/extension.rb