Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions plugin/google_analytics_ga.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# Google Analytics GA4 plugin for tDiary
#
# Copyright (C) 2025 KITADAI Yukinori <[email protected]>
# You can redistribute it and/or modify it under GPL2.
#
if /^(?:latest|day|month|nyear|search)$/ =~ @mode then
add_footer_proc do
google_analytics_ga_insert_code
end
end

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

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

# G-XXXXXXXXXX
add_conf_proc( 'google_analytics_ga', 'Google Analytics GA4' ) do
if @mode == 'saveconf' then
@conf['google_analytics_ga.profile'] = @cgi.params['google_analytics_ga.profile'][0]
end
r = <<-HTML
<h3>Google Analytics GA4 Profile</h3>
<p>set your Profile ID (G-XXXXXXXXXX)</p>
<p><input name="google_analytics_ga.profile" value="#{h @conf['google_analytics_ga.profile']}"></p>
HTML
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_ga.profile).map {|key|
@conf[key]
}.find {|profile|
profile && !profile.empty?
}
<<-HTML
<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
"vars": {
"gtag_id": "#{h profile_id}"
"config" : {
"#{h profile_id}": { "groups": "default" }
}
}
}
</script>
</amp-analytics>
HTML
end
end
59 changes: 59 additions & 0 deletions spec/google_analytics_ga_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
$:.unshift(File.dirname(__FILE__))
require 'spec_helper'

describe "google_analytics_ga plugin" do
def setup_google_analytics_ga_plugin(profile_id, mode)
fake_plugin(:google_analytics_ga) { |plugin|
plugin.mode = mode
plugin.conf['google_analytics_ga.profile'] = profile_id
}
end

describe "should render javascript" do
before do
@plugin = setup_google_analytics_ga_plugin('G-XXXXXXXXXX', 'latest')
end

it "for footer" do
snippet = @plugin.footer_proc
expect(snippet).to eq(expected_html_footer_snippet)
end
end

describe "should render javascript" do
before do
@plugin = setup_google_analytics_ga_plugin('G-XXXXXXXXXX', 'conf')
end

it "for footer" do
snippet = @plugin.footer_proc
expect(snippet).to be_empty
end
end

describe "should not render when profile_id is empty" do
before do
@plugin = setup_google_analytics_ga_plugin(nil, 'latest')
end

it "for footer" do
snippet = @plugin.footer_proc
expect(snippet).to be_empty
end
end

def expected_html_footer_snippet
expected = <<-SCRIPT
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXXXXXXXXX');
</script>
SCRIPT
expected.gsub( /^\t/, '' ).chomp
end
end