核心方法参考

本文档提供了 Embed API 核心方法的参考信息,并概述了这些方法如何与 Embed API 组件以及 Google Analytics(分析)基础客户端库互动。

核心方法

Embed API 的核心方法是 gapi.analytics 对象的方法。

ready

在 Embed API 库完全加载后立即将要调用的回调函数加入队列。回调将以添加次序为依据进行调用。

ready 函数由 Embed API 代码段定义,因此可直接使用。其他所有函数应置于 ready callback 中,以确保在库加载后再调用它们。

用法

gapi.analytics.ready(callback)

参数

名称 类型 说明
callback Function 该函数将在 Embed API 库完全加载后立即被调用。

示例

gapi.analytics.ready(function() {
  // Code in here will be invoked once the library fully loads.
});

createComponent

利用指定的名称和原型方法创建一个组件。所创建的组件将采用传递的名称存储在 gapi.analytics.ext 中。

createCallback 函数应始终在 ready callback 内接受调用,以确保加载 Embed API 客户端库。

用法

gapi.analytics.createComponent(name, prototypeMethods)

参数

名称 类型 说明
name string 组件的名称。
prototypeMethods Object 此对象的属性和方法将存储在组件原型中。

示例

gapi.analytics.ready(function() {

  gapi.analytics.createComponent('MyComponent', {
    foo: function() {
      alert('foo');
    },
    bar: function() {
      alert('bar');
    }
  });

  var myComponentInstance = new gapi.analytics.ext.MyComponent();
  myComponentInstance.foo(); // Alerts 'foo'.

});