portagenetwork / roadmap

Developed by the the Alliance in collaboration with University of Alberta, DMP Assistant a data management planning tool, forking the DMP Roadmap codebase
MIT License
6 stars 1 forks source link

Update Google Analytics Handling For Individual Organisations #678

Open aaronskiba opened 7 months ago

aaronskiba commented 7 months ago

Please complete the following fields as applicable:

What version of the DMPRoadmap code are you running? (e.g. v2.2.0)

Expected behaviour:

Actual behaviour:

aaronskiba commented 7 months ago

Corresponding code in app/views/layouts/application.html.erb:

    <% if Rails.configuration.x.google_analytics.tracker_root.present? %>
      <% root_abbr = Rails.configuration.x.google_analytics.tracker_root %>
      <% root_org = Org.where(abbreviation: root_abbr).first %>
      <% if root_org.tracker.present? %>
        <% root_code = root_org.tracker.code %>
        <!-- Google Analytics -->
        <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','https://www.google-analytics.com/analytics.js','ga');

          ga('create', '<%=root_code%>', 'auto');
          ga('send', 'pageview');
          <% if current_user.present? and current_user.org.present? %>
            <% client_org = current_user.org %>
            <% if client_org.tracker.present? %>
              <% client_code = client_org.tracker.code %>
              <% unless root_code == client_code %>
                ga('create', '<%=client_code%>', 'auto','clientTracker');
                ga('clientTracker.send', 'pageview');
              <% end %>
            <% end %>
          <% end %>
        </script>
      <% end %>
    <% end %>
    <!-- End Google Analytics -->
aaronskiba commented 7 months ago

There are a few issues here: 1) The code block is not entered because we have never provided a value for config.x.google_analytics.tracker_root within config/initializers/_dmproadmap.rb 2) The analytics.js code snippet is outdated. Google Analytics advises to [Replace the analytics.js snippet with the gtag.js snippet](https://developers.google.com/analytics/devguides/migration/ua/analyticsjs-to-gtagjs#analyticsjs_2_gtagjs) 3)app/models/tracker.rb` includes the following:

  validates :code, format: { with: /\A\z|\AUA-[0-9]+-[0-9]+\z/,
                             message: 'wrong format' }
end

This requires that the tracker code provided by an organisation have the prefix UA-. However, the current GA4 tags no longer follow this format.