混合 JS 和 PHP 变量

发布于 2025-01-20 15:14:34 字数 1077 浏览 3 评论 0原文

我正在使用MediaWiki,并希望为每个Wiki提供不同的Google Analytics(分析)标签加载,以数据库名称为基础。但是,考虑到GA标签使用JavaScript,我正在努力执行此操作,而我正在使用的数组/变量IS是PHP。

这就是我到目前为止的(正确的键=> value将根据数据库名称选择脚本,所以我不必担心):

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

然后我有之前运行的挂钩该页面加载并添加标签(这是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;
};

我不确定如何将标签从PHP数组中获取到JavaScript中?通常,如果只是JavaScript,我会替换“ CodeHere”。 鉴于变量是PHP,并且代码是JavaScript,这并不容易。

使用$ WGANALYTICSCODE,但显然,

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我还不会笑 2025-01-27 15:14:34

您可以使用使用单词将数组传递到匿名函数

示例

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

之后,您可以在功能主体内使用数组,

我希望它有用

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文