class Jekyll::AttendeasePlugin::AnalyticsGoogleAnalyticsGtagTag

Public Instance Methods

render(context) click to toggle source
# File lib/jekyll/attendease_plugin/tags.rb, line 292
      def render(context)
        site_settings = context.registers[:site].data['site_settings'].clone
        analytics = site_settings['analytics']

        has_analytics_id = !analytics.nil? && !analytics['googleAnalyticsTrackingId'].nil? && !analytics['googleAnalyticsTrackingId'].empty?
        has_adwords_id = !analytics.nil? && !analytics['googleAnalyticsAdwordsId'].nil? && !analytics['googleAnalyticsAdwordsId'].empty?
        return '' if analytics.nil? || (!has_analytics_id && !has_adwords_id)

        gtag_id = has_analytics_id ? analytics['googleAnalyticsTrackingId'] : analytics['googleAnalyticsAdwordsId']

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

#{ has_analytics_id ? "  gtag('config', '#{analytics['googleAnalyticsTrackingId']}');" : ''}
#{ has_adwords_id ? "  gtag('config', '#{analytics['googleAnalyticsAdwordsId']}');" : ''}
</script>
_EOT
        script
      end