إعادة تسمية كائن ga

في بعض الحالات، قد ترغب في إضافة analytics.js إلى صفحتك، ولكن يتم استخدام المتغيّر ga حاليًا لصفحة أخرى. للتعامل مع هذا الأمر، يوفر analytics.js آلية لإعادة تسمية كائن ga العام.

إعادة تسمية الكائن العمومي

تسمح لك علامة "إحصاءات Google" بإعادة تسمية كائن ga العام من خلال تغيير المعلمة النهائية التي يتم تمريرها إلى الدالة المصغّرة. عليك أيضًا تعديل جميع عمليات الاستدعاء في قائمة انتظار الأوامر من ga() إلى أي اسم تختاره.

على سبيل المثال، إذا أردت إعادة تسمية الكائن ga ليصبح analytics، يمكنك تغيير العلامة على النحو التالي:

<!-- Google Analytics -->
<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','//www.google-analytics.com/analytics.js','analytics');

analytics('create', 'UA-XXXXX-Y', 'auto');
analytics('send', 'pageview');
</script>
<!-- End Google Analytics -->

إعادة تسمية الكائن العمومي يدويًا

وعملية إعادة تسمية العنصر العمومي تعمل لأن analytics.js، عند تحميلها، تبحث عن سلسلة مخزّنة في متغير عمومي يُسمى GoogleAnalyticsObject. إذا وجد هذا المتغير، فإنه يستخدم اسم السلسلة كاسم جديد لقائمة انتظار الأوامر العامة.

على سبيل المثال، إذا كنت تستخدم طريقة $.getScript في jQuery لتحميل analytics.js، يمكنك إعادة تسمية العنصر العمومي باستخدام الرمز التالي:

<script>
// Instructs analytics.js to use the name `analytics`.
window.GoogleAnalyticsObject = 'analytics';

// Use jQuery to load analytics.js.
$.getScript('//www.google-analytics.com/analytics.js', function() {

  // Creates a tracker and sends a pageview using the renamed command queue.
  analytics('create', 'UA-12345-1', 'auto');
  analytics('send', 'pageview');
});
</script>

العلامة غير المتزامنة البديلة

على عكس علامة "إحصاءات Google" العادية، لا توفّر علامة غير متزامنة بديلة دعمًا تلقائيًا لإعادة تسمية كائن ga العام.

مع ذلك، باستخدام الأسلوب الموضّح أعلاه، يمكنك إعادة تسمية عنصر ga العام والاستمرار في الحصول على جميع مزايا التحميل المُسبق للعلامة البديلة غير المتزامنة.

إنّ النسخة المعدَّلة التالية من العلامة غير المتزامنة البديلة تضبط المتغيّر GoogleAnalyticsObject على analytics وتُعيد تسمية جميع مثيلات ga إلى analytics أيضًا:

<!-- Google Analytics -->
<script>

// Instructs analytics.js to use the name `analytics`.
window.GoogleAnalyticsObject = 'analytics';

// Creates an initial analytics() function.
// The queued commands will be executed once analytics.js loads.
window.analytics = window.analytics || function() {
  (analytics.q = analytics.q || []).push(arguments)
};

// Sets the time (as an integer) this tag was executed.
// Used for timing hits.
analytics.l = +new Date;

// Creates a default analytics object with automatic cookie domain configuration.
analytics('create', 'UA-12345-1', 'auto');

// Sends a pageview hit from the analytics object just created.
analytics('send', 'pageview');
</script>

<!-- Sets the `async` attribute to load the script asynchronously. -->
<script async src='//www.google-analytics.com/analytics.js'></script>
<!-- End Google Analytics -->