The X for Websites JavaScript fires events on initialization and after a viewer interacts with a widget. Enhance your application and analytics by tapping into widget events. Note that Web Intent events fire when the interaction occurs, not after the action completes. Bind a X widget event in your JavaScript code by registering a callback function withDocumentation Index
Fetch the complete documentation index at: https://docs.x.com/llms.txt
Use this file to discover all available pages before exploring further.
twttr.events.bind.
The Intent Event Object
When a detectable action occurs within a Web Intent or widget, an object representing the event is passed to your JavaScript callback. Intent Event objects include the following data:Intent event object properties
| Property | Data Type | Description |
|---|---|---|
target | HTMLElement | The DOM node where the widget is instantiated. Most likely an iframe, but may also be the original embed code element if the widget failed to initialize, or another sandboxed element. Use this value to differentiate between different intents or buttons on the same page. |
region | String | Extended detail indicating where in a widget the event originated. For example, button or count components of Post button or Follow button integrations, or Post actions within an embedded Post. |
data | Object | Key/value pairs relevant to the event. |
Waiting for Asynchronous Resources
Loading the widgets.js file asynchronously will require you to wait before binding events: the functions you are calling do not yet exist. You will need to wrap your event bindings in a callback function such as the twttr.ready asynchronous function queue which will be invoked once everything has loaded. All event examples below assume you have wrapped those event bindings in this callback.Available Events
loaded
Occurs aftertwttr.widgets.load has initialized widgets in a page. Includes an array of references to the newly created widget nodes.
rendered
Occurs after an individual widget in a page is rendered. Includes a reference to the newly created widget node. Occurs at the same time asloaded, but for each individual widget. Also triggered when creating a widget with a factory function.
tweet
This event will be triggered when the user clicks a Post Web Intent. Includes a Post in reply to an embedded Post or a Post displayed in an embedded Timeline.follow
This event will populate the followeduser_id or screen_name in the event object’s data argument, depending on the parameters you provided in the web intent.
retweet
This event will populate the original Post that was reposted’ssource_tweet_id in the event object’s data argument.
like
This event will populate the likedtweet_id in the event object’s data argument.
click
Receive an event when the user invokes a Web Intent from within an embedded widget.Example: Detecting Events for Web Analytics
It’s easy to capture these events and pipe them to your web analytics solution. Please note that you’ll need to be using HTTP or HTTPs protocols on the hosting page for these events.Supplemental Events Resources and Examples
- Google Analytics Event Tracking Guide
- Google Analytics SocialWidgetTracker plugin
- X Intent Events with Google Analytics using _gaq.push(); by @nulluk
- An optimized approach to Google Analytics integration by @yahel