diff options
-rw-r--r-- | layouts/head.html | 10 | ||||
-rw-r--r-- | lib/helpers/generic.rb | 22 | ||||
-rw-r--r-- | spec/lib/helpers/generic_spec.rb | 76 |
3 files changed, 108 insertions, 0 deletions
diff --git a/layouts/head.html b/layouts/head.html index 758ae022..002c0516 100644 --- a/layouts/head.html +++ b/layouts/head.html @@ -85,3 +85,13 @@ <link rel="apple-touch-icon" href="/assets/images/apple-touch-icon.png?v=2"> <link rel="manifest" href="/assets/manifests/site.webmanifest"> <meta name="theme-color" content="#FC6D26"> + +<% if gitlab_analytics_enabled? %> + <script src="https://cdn.jsdelivr.net/npm/@gitlab/application-sdk-browser@0.2.8/dist/gl-sdk.min.js"></script> + <script> + window.glClient = window.glSDK.glClientSDK(<%= gitlab_analytics_json %>); + + window.glClient?.page(); + </script> + +<% end %> diff --git a/lib/helpers/generic.rb b/lib/helpers/generic.rb index 5132cdaf..4fa01875 100644 --- a/lib/helpers/generic.rb +++ b/lib/helpers/generic.rb @@ -212,5 +212,27 @@ module Nanoc::Helpers warn("Error getting release dates - #{e}") "[]" end + + # This method will check whether gitlab product analytics is + # enabled or not based on env variables + # + def gitlab_analytics_enabled? + !ENV.fetch('GITLAB_ANALYTICS_HOST', '').empty? && + !ENV.fetch('GITLAB_ANALYTICS_ID', '').empty? + end + + # + # This method will return configuration object + # when gitlab product analytics is enabled. + # + def gitlab_analytics_json + return unless gitlab_analytics_enabled? + + { + 'appId' => ENV.fetch('GITLAB_ANALYTICS_ID', nil), + 'host' => ENV.fetch('GITLAB_ANALYTICS_HOST', nil), + 'hasCookieConsent' => true + }.to_json + end end end diff --git a/spec/lib/helpers/generic_spec.rb b/spec/lib/helpers/generic_spec.rb index a96f05d4..569cdc81 100644 --- a/spec/lib/helpers/generic_spec.rb +++ b/spec/lib/helpers/generic_spec.rb @@ -259,4 +259,80 @@ RSpec.describe Nanoc::Helpers::Generic do expect(subject).to eq("[]") end end + + describe '#gitlab_analytics_enabled?' do + context 'when GITLAB_ANALYTICS_HOST and GITLAB_ANALYTICS_ID are set' do + before do + ENV['GITLAB_ANALYTICS_HOST'] = 'https://collector.com' + ENV['GITLAB_ANALYTICS_ID'] = '123' + end + + it 'returns true' do + expect(mock_class.gitlab_analytics_enabled?).to be_truthy + end + end + + context 'when only GITLAB_ANALYTICS_HOST is set' do + before do + ENV['GITLAB_ANALYTICS_HOST'] = 'https://collector.com' + ENV['GITLAB_ANALYTICS_ID'] = nil + end + + it 'returns false' do + expect(mock_class.gitlab_analytics_enabled?).to be_falsey + end + end + + context 'when only GITLAB_ANALYTICS_ID is set' do + before do + ENV['GITLAB_ANALYTICS_HOST'] = nil + ENV['GITLAB_ANALYTICS_ID'] = '123' + end + + it 'returns false' do + expect(mock_class.gitlab_analytics_enabled?).to be_falsey + end + end + + context 'when neither GITLAB_ANALYTICS_HOST nor GITLAB_ANALYTICS_ID are set' do + before do + ENV['GITLAB_ANALYTICS_HOST'] = nil + ENV['GITLAB_ANALYTICS_ID'] = nil + end + + it 'returns false' do + expect(mock_class.gitlab_analytics_enabled?).to be_falsey + end + end + end + + describe '#gitlab_analytics_json' do + context 'when gitlab analytics is enabled' do + before do + ENV['GITLAB_ANALYTICS_ID'] = '123' + ENV['GITLAB_ANALYTICS_HOST'] = 'https://collector.com' + allow(mock_class).to receive(:gitlab_analytics_enabled?).and_return(true) + end + + it 'returns the configuration object' do + expected_json = { + 'appId' => '123', + 'host' => 'https://collector.com', + 'hasCookieConsent' => true + }.to_json + + expect(mock_class.gitlab_analytics_json).to eq(expected_json) + end + end + + context 'when gitlab analytics is not enabled' do + before do + allow(mock_class).to receive(:gitlab_analytics_enabled?).and_return(false) + end + + it 'returns nil' do + expect(mock_class.gitlab_analytics_json).to be_nil + end + end + end end |