title: Mixing JS and PHP variables
body: <p>I'm using MediaWiki and looking to have a different Google Analytics tag load for each wiki dependent on the database name. I'm struggling with how to do this, however, given the GA tag uses JavaScript and the array/variable I'm using is in PHP.</p>
  <p>This is what I have thus far (the correct key =&gt; value will already be chosen by the script earlier based on the database name, so I don't have to worry about that):</p>
  <pre><code> $wgAnalyticsCode =&gt; [
     'wiki1' =&gt; '(code 1)',
     'wiki2' =&gt; '(code 2)',
     'wiki3' =&gt; '(code 3)'
   ]
  </code></pre>
  <p>and then I have the hook that runs before the page loads and adds the tag (which is a mix of PHP/JavaScript:</p>
  <pre><code>$wgHooks['BeforePageDisplay'][] = function( OutputPage &amp;$out, Skin &amp;$skin ) {
       $code = &lt;&lt;&lt;HTML
  &lt;!-- Global site tag (gtag.js) - Google Analytics --&gt;
  &lt;script async src=&quot;https://www.googletagmanager.com/gtag/js?id=CODEHERE&quot;&gt;&lt;/script&gt;
  &lt;script&gt;
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
  
    gtag('config', 'CODEHERE');
  &lt;/script&gt;
  HTML;
  
       $out-&gt;addHeadItem( 'gtag-insert', $code );
       return true;
  };
  </code></pre>
  <p>I'm not entirely sure how to get the tag from the PHP array into the JavaScript? Usually, if it was just JavaScript, I would replace &quot;CODEHERE&quot; with <code>$wgAnalyticsCode</code>, but obviously it isn't that easy given that the variable is PHP and the code is JavaScript.</p>
  <p>Any advice would be appreciated?</p>