Rudloff / alltube

Web GUI for youtube-dl
GNU General Public License v3.0
2.94k stars 584 forks source link

How to easily add google analytics code into the header? #375

Closed anthonyinit closed 2 years ago

anthonyinit commented 2 years ago

New issue

Your environment

Please answer these questions when reporting a new issue:

*What is your operating system (Windows, Linux, OSX, etc.)?Debian 10*

*What is your web server (Apache, IIS, etc.)?Nginx 1.9*

*What version of AllTube are you using?v3.0*

*How did you install AllTube (with Git or with a release package)?Git*

*What version of PHP are you using?7.3*

*What version of Python are you using?Python 2.7.16*

What version of youtube-dl are you using?

Do you get any PHP-related errors in your webserver's logs?

What is the content of your config/config.yml file?

# Insert content here.

Please provide the URL of a video that causes the issue.

Describe your issue

Hello, How can I add google analytic code in the header? I try to use it with {literal}{/literal} that didn't help me at all

I heard that I can use google analytics code into a separate js file and just referenced that js file from the template but i have no idea how to do this

Thanks

Rudloff commented 2 years ago

This is not supported but if you are forking AllTube, you can easily add you script directly in the template:

diff --git a/templates/inc/head.tpl b/templates/inc/head.tpl
index 4e5d68d..b975fae 100644
--- a/templates/inc/head.tpl
+++ b/templates/inc/head.tpl
@@ -26,6 +26,10 @@
     {if isset($debug_render)}
         {$debug_render->renderHead()}
     {/if}
+
+    <script>
+        // Add your script here.
+    </script>
 </head>
 <body>
 <div class="page {$class}">
anthonyinit commented 2 years ago

@Rudloff Thank you for your reply. yes this is how I add in the first place and if I used the code without {literal} {/literal} I will get this error

Error when starting the app: Syntax error in template "file:/www/wwwroot/domaindemo.com/templates/inc/head.tpl" on line 33 "function gtag(){dataLayer.push(arguments);}" - Unexpected ".", expected one of: "}"

But if I add with using {literal} {/litarel}

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

  gtag('config', 'UA-xxxxxxxxxxxx-1');
</script>
{/literal}

Everything is okay and the site won't crash but strangely google analytics never detects any visitors. kinda confused about what's is wrong

anthonyinit commented 2 years ago

It looks like something security related. analytics code is blocked

Refused to load the script 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-1' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback.

www.xxxxxxxxxxxxxxx.com/:29 Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'none'". Either the 'unsafe-inline' keyword, a hash ('sha256-lgs05LcxPCy6b/nKh1emNJ75Di04VZkIvpIeIiIfqdU='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

www.xxxxxxxxxxxxxxxx.com/:1 Refused to load the script 'https://www.xxxxxxxxxxxxxxxx.com/cdn-cgi/challenge-platform/h/g/scripts/invisible.js' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback.

www.xxxxxxxxxxxxxxx.com/:1 Refused to load the script 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXXX-1' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'script-src-elem' was not explicitly set, so 'default-src' is used as a fallback.
Rudloff commented 2 years ago

AllTube uses a CSP to restrict which scripts can be loaded. If you want to load other scripts, you will have to adapt this middleware (or disable it): https://github.com/Rudloff/alltube/blob/eeda434b2fd61be1a9b4a49c35d5bda79fae1e78/classes/Middleware/CspMiddleware.php#L37

anthonyinit commented 2 years ago

@Rudloff Thank you again for your reply appreciate it.

Not much of a coding expert I tried a few methods with the help of google search but I get errors and the site crashed. so I had to disable it. I hope I'm not doing anything wrong or if you can tell me what exactly I should put in the code would be highly appeciated.

// ->addDirective('default-src', [])

anthonyinit commented 2 years ago

All working now @Rudloff Thank you

hhhkkk520 commented 2 years ago

Hello, @anthonyinit

Could you please write down the detailed process of solving this problem? I have the same need but I don't know how to do it

anthonyinit commented 2 years ago

Hello, @anthonyinit

Could you please write down the detailed process of solving this problem? I have the same need but I don't know how to do it

Hello,

Comment the above line, please.