0

I have faced a problem that I can't understand. Can anyone please help me?

I want to add Google-Analytics metrics to my small project app. I have added gtag script, rails encrypted credentials and set up on Heroku.

It works and tracks when I put my UA manually like that:

 <script async src="https://www.googletagmanager.com/gtag/js?id=UA-18929....-2"></script>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>

Then when I run in terminal:

EDITOR="subl --wait" bin/rails credentials:edit

I have added credentials this way:

google_analytics:
  google_analytics: UA-18929....-2

When I run the dig command in terminal to see the credentials in rails console it works:

Rails.application.credentials.dig(:google_analytics, :google_analytics)

But then when I replace

=UA-18929....-2" 

with

<%= Rails.application.credentials.dig(:google_analytics, :google_analytics) %>

the Google Analytics do not update data. Can please anyone tell me why and how to set it up correctly?

I run both of them locally and when I hardcore my UA it works, when I dig the credentials it doesn't.

I have also tried to print my credentials this way in view:

<%= Rails.application.credentials.as_json %>

and I'm getting empty hash back, it shouldn't work that way I guess.

  • 1
    Welcome to SO! Please include snippets of output and code at text in your question rather than as images. It makes it difficult for people to help you and makes it hard for people with similar issues to find your question. – rmlockerd Feb 15 '21 at 19:57
  • Do not update data but ID is present or not? – Hubert Jakubiak Feb 15 '21 at 20:31
  • There's nothing wrong with your credentials line. Something else is broken. Did you save it to your credentials properly? Have you installed the `master.key` properly? Have you correctly configured Google Analytics? – Jon Feb 15 '21 at 22:40
  • I have updated my post, hope it's better now, sorry for the images! HubertJakubiak sorry Hubert, I don't understand a question, I'm a bit newbie, good guide on Rspec tho! @Jon It's a bit hard to tell for me as I'm a newb, to add credentials I have run in terminal: EDITOR="subl --wait" bin/rails credentials:edit then put it this way: google_analytics: google_analytics: UA-18929....-2 and saved, closed the file. I have master.key added in my project and ignored on GitHub, do I need to configure it other way? The google analytics works with hardcoded but not credentials – Mateusz Tomczak Feb 16 '21 at 08:12
  • Do you have both config/credentials.yml.enc and a config/credentials directory? If so, the environment-specific files in your credentials directory will completely override anything you write to config/credentials.yml.enc using the terminal command you specify in your post. – obiruby Feb 16 '21 at 08:38
  • @obiruby No I have only credentials.yml.enc, there is no credentials folder in config as you can see here: https://github.com/tmtocb/instagramclone-TDD/tree/master/config – Mateusz Tomczak Feb 16 '21 at 09:25

1 Answers1

0

I have actually made it work, it was a very basic mistake, first I decided to skip dig and access credentials this way:

Rails.application.credentials.google_analytics

I was actually using Rails credentials as ERB in JS file after I moved my analytics to src folder... I decided to skip this stage and moved back my script into layout head. Works now!