Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Google Analytics JS code to GA4 #201

Merged
merged 1 commit into from
Apr 11, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

## Unreleased

* Update Google Analytics JS code to GA4. (Matthew Bass)

* Upgrade to Sinatra 3.1. (なつき)

* Tilt renderers (e.g. for Markdown, Haml, or Textile) are now configurable.
4 changes: 2 additions & 2 deletions templates/config/config.yml
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ content: content
# scroll down a bit to set it in production even if you're not deploying
# to your own server.
#
# google_analytics_code: "UA-???????-?"
# google_analytics_code: "G-??????????"

# read_more
# When the summary of an article is displayed on the home page, or
@@ -64,7 +64,7 @@ content: content

# production:
# content: /var/apps/nesta/shared/content
# google_analytics_code: "UA-???????-?"
# google_analytics_code: "G-??????????"

# The following settings control the behaviour of the `nesta build`
# command, which is used to generate a "static" version of your site.
2 changes: 1 addition & 1 deletion test/integration/default_theme_test.rb
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@ def in_categories(*categories)
end

it 'includes Google Analytics JavaScript when configured' do
analytics_code = { 'google_analytics_code' => 'UA-1234' }
analytics_code = { 'google_analytics_code' => 'G-1234' }
stub_config(temp_content.merge('test' => analytics_code)) do
visit '/'
assert_nil all('script').find { |s| s[:src].match /analytics\.js/ }
12 changes: 6 additions & 6 deletions views/analytics.haml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
- if @google_analytics_code
:plain
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=#{@google_analytics_code}"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

ga('create', '#{@google_analytics_code}', 'auto');
ga('send', 'pageview');
gtag('config', '#{@google_analytics_code}');
</script>