Skip to content

Commit

Permalink
Allow to inject google tag manager code instead of google analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
oltarasenko committed Oct 12, 2020
1 parent 1b9d349 commit 0f9c46a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/crawly_ui_web/templates/layout/root.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<%= csrf_meta_tag() %>
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<%= raw ga_tag() %>
<%= raw ga_js_tag() %>
</head>

<body>

<%= raw ga_nojs_tag() %>
<div id="__next">
<div class="Page" data-reactroot="">
<header class="Header">
Expand Down
31 changes: 22 additions & 9 deletions lib/crawly_ui_web/views/layout_view.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
defmodule CrawlyUIWeb.LayoutView do
use CrawlyUIWeb, :view

def ga_tag() do
def ga_js_tag() do
case System.get_env("GA_TRACKING") do
nil ->
""

tracking_code ->
"""
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=#{tracking_code}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','#{tracking_code}');</script>
<!-- End Google Tag Manager -->
"""
end
end

gtag('config', '#{tracking_code}');
</script>
def ga_nojs_tag() do
case System.get_env("GA_TRACKING") do
nil ->
""

tracking_code ->
"""
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=#{tracking_code}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
"""
end
end
Expand Down

0 comments on commit 0f9c46a

Please sign in to comment.