本文档提供了 Embed API 核心方法的参考信息,并概述了这些方法如何与 Embed API 组件以及 Google Analytics(分析)基础客户端库互动。
核心方法
Embed API 的核心方法是 gapi.analytics
对象中的方法。
ready
在 Embed API 库完全加载后立即将要调用的回调函数加入队列。回调将以添加次序为依据进行调用。
ready
函数由 Embed API 代码段定义,因此可立即使用。所有其他函数应放置在 ready
回调内,以确保在调用库之前对其进行加载。
用量
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
中。
应始终在 ready callback 内调用 createCallback
函数,以确保加载 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'. });