fhdsl / Overleaf_and_LaTeX_for_Scientific_Articles

The course covers basic information about why LaTeX can be useful, how to get started in Overleaf using LaTeX with a template, how to work with a team on Overleaf, and what to do when you encounter problems.
https://hutchdatascience.org/Overleaf_and_LaTeX_for_Scientific_Articles/
Creative Commons Attribution 4.0 International
0 stars 0 forks source link

Google Analytics not working #45

Open carriewright11 opened 9 months ago

carriewright11 commented 9 months ago

Describe what is not working with the course

The Google Analytics may not be working as well - a couple of weeks ago I moved it in a commit. Can you please fix this @howardbaek ?

carriewright11 commented 9 months ago

This was the PR https://github.com/fhdsl/Overleaf_and_LaTeX_for_Scientific_Articles/pull/42

howardbaek commented 9 months ago

The Google Analytics ID is correct. I see the difference is where the GA_Script.Rhtml file is included in _output.yml. I see that you had tried in_header: GA_Script.Rhtml and you weren't able to render locally. Maybe we can stick the GA_Script.Rhtml next to before_body?

before_body: [GA_Script.Rhtml, assets/big-image-itcr.html]

carriewright11 commented 9 months ago

@howardbaek can you please check if this new location fixed it?

howardbaek commented 9 months ago

I see the PR corresponding to this issue was merged, so closing this issue.

carriewright11 commented 9 months ago

@howardbaek does this mean we confirmed that the new location works for us to see interactions now on google analytics?

howardbaek commented 9 months ago

I checked the google analytics for this course, and it doesn't seem to be working.

howardbaek commented 9 months ago

I've not deeply looked into how OTTR renders bookdown (do you know @cansavvy?), but maybe the reason is that I had only made the changes and didn't rmarkdown::render_site(encoding = 'UTF-8')?

That's what I do in this commit https://github.com/fhdsl/Overleaf_and_LaTeX_for_Scientific_Articles/commit/51a01113e0930cf5ee92b2ebd20cfbb45542b42f

howardbaek commented 9 months ago

All these courses below put GA_Script.Rhtml inside before_body,

And their GAs are responsive, so I'm assuming that's the correct location. However, I've noticed a couple other websites with the GA_Script inside in_header.

In the first website, the GA Script is named GA_Script.Rhtml. In the second website, its named GA_Script.html. Strange that there are two different extensions.

@carriewright11 Do you remember what you were seeing when rendering locally?

howardbaek commented 9 months ago

(I wasn't involved in the original development of OTTR, so this may be why I'm confused.)

Why do the GA_Script.html files in OTTR-generated websites contain html elements in addition to the Google tag code, seen below:

  <!-- Global site tag (gtag.js) - Google Analytics -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=G-9PPWML6VEE"></script>
  <script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());

    gtag('config', 'G-9PPWML6VEE');
  </script>

To my understanding, the above code needs to be placed before the closing <head> tag (https://stackoverflow.com/questions/41376989/how-to-include-google-analytics-in-an-rmarkdown-generated-github-page) and the way we do this in bookdown is using the in_header option (https://bookdown.org/yihui/bookdown/yaml-options.html).

If we include the above code inside the <head> element of our rendered html pages (using the in_header option), do we need the additional html elements like <body> or <html> in the GA_Script.html files?

carriewright11 commented 9 months ago

Hi @howardbaek - so glad you are looking at this and figuring out the consistency differences and what is needed.

@carriewright11 Do you remember what you were seeing when rendering locally?

This was the error: Error in if (title == x2) return(head) : the condition has length > 1 Calls: ... split_chapters -> build -> sub -> is.factor -> prepend_chapter_title Execution halted`

I tried running it locally with the in_header option again and reproduced the error and indeed if I remove <body> and <html> it renders and the error goes away. Although possibly redundant, this did not cause an error before - so the way in_header is handled may have changed?

howardbaek commented 9 months ago

@carriewright11 What function/button did you run when you ran it locally?

A possibly separate question: I am digging deep into OTTR and bookdown and it seems like we are using the "Knit and Merge" (K-M) method to render a book since we set new_session: yes in this line. Is there a specific reason we are using the K-M method instead of the M-K method?