Page MenuHomePhabricator

How can I include any JS code in Phabricator
OpenPublic

Asked by AlexL777 on Feb 21 2017, 1:18 PM.

Details

Hello Community and Developers,

I would like to incorporate Javascript code in Phabricator like Adsense or Analytics and found so far no solution, how I can realize this. Probably this is not possible or am I mistaken?

So my question? Does it work with the inclusion of any JS code in Phabricator?

Thanks in advance.

Answers

This answer has been hidden.

chad
Updated 1,548 Days Ago

Does this work?

diff --git a/src/view/page/PhabricatorBarePageView.php b/src/view/page/PhabricatorBarePageView.php
index ca9d432..1095a97 100644
--- a/src/view/page/PhabricatorBarePageView.php
+++ b/src/view/page/PhabricatorBarePageView.php
@@ -127,6 +127,15 @@ class PhabricatorBarePageView extends AphrontPageView {
         'content' => 'never',
       ));
 
+    $google = phutil_safe_html(
+    "<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
+
+  ga('create', 'UA-google-user-id-here', 'auto');
+  ga('send', 'pageview');</script>");
+
     $response = CelerityAPI::getStaticResourceResponse();
 
     if ($this->getRequest()) {
@@ -142,7 +151,7 @@ class PhabricatorBarePageView extends AphrontPageView {
 
     $developer = PhabricatorEnv::getEnvConfig('phabricator.developer-mode');
     return hsprintf(
-      '%s%s%s%s%s%s%s%s%s',
+      '%s%s%s%s%s%s%s%s%s%s',
       $viewport_tag,
       $mask_icon,
       $icon_tag_76,
@@ -150,6 +159,7 @@ class PhabricatorBarePageView extends AphrontPageView {
       $icon_tag_152,
       $favicon_tag,
       $referrer_tag,
+      $google,
       CelerityStaticResourceResponse::renderInlineScript(
         $framebust.jsprintf('window.__DEV__=%d;', ($developer ? 1 : 0))),
       $response->renderResourcesOfType('css'));

New Answer