Hi,
I would like to add to my phoenix site the google analytics javascript code, how could enable it only in production?
Which type of if I need to add to the template?
<script async src="https://www.googletagmanager.com/gtag/js?id=xyz"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'xyz');
</script>
Thanks
1 Like
You can include the analytics <script>
tags conditionally to some configuration option. Then turn it on in config/prod.exs
and not in other environments.
I add these lines
# config/releases.exs
config :my_app, env: :prod
# my_app_web/templates/layout/app.html.eex
<%= if Application.get_env(:my_app, :env) == :prod do %>
... google analytics
<% end %>
2 Likes
Maybe use some more descriptive config key though. :myapp, :env
is hardly better than Mix.env()
. E.g. config :my_app, analytics: true
8 Likes