Class: Jekyll::AttendeasePlugin::AnalyticsGoogleAnalyticsGtagTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/attendease_plugin/tags.rb

Instance Method Summary collapse

Instance Method Details

#render(context) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/jekyll/attendease_plugin/tags.rb', line 287

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 = "<!-- Global Site Tag (gtag.js) - Google Analytics -->\n<script async src=\"https://www.googletagmanager.com/gtag/js?id=\#{gtag_id}\"></script>\n<script>\n  window.dataLayer = window.dataLayer || [];\n  function gtag(){dataLayer.push(arguments);}\n  gtag('js', new Date());\n\n\#{ has_analytics_id ? \"  gtag('config', '\#{analytics['googleAnalyticsTrackingId']}');\" : ''}\n\#{ has_adwords_id ? \"  gtag('config', '\#{analytics['googleAnalyticsAdwordsId']}');\" : ''}\n</script>\n"
  script
end