Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

93
Views
Mixing JS and PHP variables

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.

This is what I have thus far (the correct key => value will already be chosen by the script earlier based on the database name, so I don't have to worry about that):

 $wgAnalyticsCode => [
   'wiki1' => '(code 1)',
   'wiki2' => '(code 2)',
   'wiki3' => '(code 3)'
 ]

and then I have the hook that runs before the page loads and adds the tag (which is a mix of PHP/JavaScript:

$wgHooks['BeforePageDisplay'][] = function( OutputPage &$out, Skin &$skin ) {
     $code = <<<HTML
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=CODEHERE"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'CODEHERE');
</script>
HTML;

     $out->addHeadItem( 'gtag-insert', $code );
     return true;
};

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 "CODEHERE" with $wgAnalyticsCode, but obviously it isn't that easy given that the variable is PHP and the code is JavaScript.

Any advice would be appreciated?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you can use the use word to pass your array to the anonymous function

Example

$wgHooks['BeforePageDisplay'][] = function (OutputPage &$out, Skin &$skin) use ($wgAnalyticsCode) {

after that you can use your array inside the function body

i hope it's helpful

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!