File: google_analytics.rb

package info (click to toggle)
tdiary-contrib 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,772 kB
  • sloc: ruby: 17,305; javascript: 8,263; lisp: 562; xml: 451; php: 61; sql: 40; makefile: 18
file content (72 lines) | stat: -rw-r--r-- 2,115 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#
# Google Analytics plugin for tDiary
#
# Copyright (C) 2005 TADA Tadashi <sho@spc.gr.jp>
# You can redistribute it and/or modify it under GPL2.
#
if /^(?:latest|day|month|nyear|search)$/ =~ @mode then
	add_footer_proc do
		google_analytics_insert_code
	end
end

def google_analytics_insert_code
	return '' unless @conf['google_analytics.profile']
	<<-HTML
		<!-- Global site tag (gtag.js) - Google Analytics -->
		<script async src="https://www.googletagmanager.com/gtag/js?id=UA-#{@conf['google_analytics.profile']}"></script>
		<script>
			window.dataLayer = window.dataLayer || [];
			function gtag(){dataLayer.push(arguments);}
			gtag('js', new Date());

			gtag('config', 'UA-#{@conf['google_analytics.profile']}');
		</script>
	HTML
end

# UA-53836-1
add_conf_proc( 'google_analytics', 'Google Analytics' ) do
	if @mode == 'saveconf' then
		@conf['google_analytics.profile'] = @cgi.params['google_analytics.profile'][0]
		@conf['google_analytics.amp.profile'] = @cgi.params['google_analytics.amp.profile'][0]
	end
	r = <<-HTML
		<h3>Google Analytics Profile</h3>
		<p>set your Profile ID (NNNNN-N)</p>
		<p>UA-<input name="google_analytics.profile" value="#{h @conf['google_analytics.profile']}"></p>
	HTML
	if defined? AMP
		r << <<-HTML
			<h3>Google Analytics Profile for AMP page</h3>
			<p>set your Profile ID (NNNNN-N) for AMP page</p>
			<p><input name="google_analytics.amp.profile" value="#{h @conf['google_analytics.amp.profile']}"></p>
		HTML
	end
	r
end

if defined? AMP
	add_amp_header_proc do
		%Q|<script async custom-element="amp-analytics"
			src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>|
	end

	add_amp_body_enter_proc do
		profile_id = %w(google_analytics.amp.profile google_analytics.profile).map {|key|
			@conf[key]
		}.find {|profile|
			profile && !profile.empty?
		}
		<<-HTML
			<amp-analytics type="googleanalytics" id="analytics1">
			<script type="application/json">
			{
				"vars": { "account": "UA-#{h profile_id}" },
				"triggers": { "trackPageview": { "on": "visible", "request": "pageview" } }
			}
			</script>
			</amp-analytics>
		HTML
	end
end