The Customer Data Platform for Developers
Website
·
Documentation
·
Community Slack
The RudderStack Plugin for Gatsby.
This is a way for you to quickly and easily get RudderStack up and running in your Gatsby application.
Questions? Please join our Slack channel or read about us on Product Hunt.
Why Use This plugin
This plugin makes it easy to integrate your Gatsby website with the RudderStack API and easily track events.
Key Features
- use multiple write keys (one for prod env, another optional one for dev)
- disable page view tracking (just in case you want to add it later manually)
- up to date (RudderStack team maintained)
Install
- NPM:
$ npm install --save gatsby-plugin-rudderstack
- YARN:
$ yarn add gatsby-plugin-rudderstack
In your gatsby-config.js file:
plugins: [
{
resolve: `gatsby-plugin-rudderstack`,
options: {
prodKey: `RUDDERSTACK_PRODUCTION_WRITE_KEY`,
devKey: `RUDDERSTACK_DEV_WRITE_KEY`,
trackPage: false,
trackPageDelay: 50,
dataPlaneUrl: `https://override-rudderstack-endpoint`,
controlPlaneUrl: `https://override-control-plane-url`,
delayLoad: false,
delayLoadTime: 1000,
manualLoad: false,
sdkURL: `https://subdomain.yourdomain.com/v3`,
loadType: 'default',
loadOptions: {
...
}
}
}
];
The identify()
method allows you to link users and their actions to a specific userid.
A sample example of how the identify()
method works in Gatsby is as shown:
class CallToAction extends React.Component {
_handleCallToAction() {
window.rudderanalytics.identify(
'12345',
{
email: 'name@domain.com',
},
{
page: {
path: '/post',
referrer: 'internal',
search: '',
title: 'Post Page',
url: '',
},
},
);
}
render() {
return (
<Link onClick={this._handleCallToAction} to="/write-post">
Write a Post
</Link>
);
}
}
In the above example, information such as the user ID, email along with contextual information such as IP address, anonymousId, etc. will be captured.
NOTE: There is no need to call identify()
for anonymous visitors to your website. Such visitors are automatically assigned an anonymousId
.
The track()
method allows you to track any actions that your users might perform.
A sample example of how the track()
method works is as shown:
window.rudderanalytics.track(
'test track event GA3',
{
revenue: 30,
currency: 'USD',
user_actual_id: 12345,
},
() => {
console.log('in track call');
},
);
In the above example, the method tracks the event ‘test track event GA3’, and information such as the revenue, currency, anonymousId.
You can use this method to track various other success metrics for your website, such as user signups, item purchases, article bookmarks, and much more.
NOTE: To override contextual information, for ex: anonymizing IP and other contextual fields like page properties, the following template can be used. Similarly one can override the auto-generated anonymousId with provided ID. For this:
window.rudderanalytics.track(
'test track event GA3',
{
revenue: 30,
currency: 'USD',
user_actual_id: 12345,
},
() => {
console.log('in track call');
},
);
Track Pageviews
If you want to track pageviews automatically, set trackPage
to true
in your gatsby-config.js
file. What we mean by "automatically" is that whenever there is a route change, we leverage Gatsby's onRouteUpdate
API in the gatsby-browser.js
file (link) to invoke window.rudderanalytics.page()
on each route change. But if you want to pass in properties along with the pageview call (ie, it's common to see folks pass in some user or account data with each page call), then you'll have to set trackPage: false
and call it yourself in your gatsby-browser.js
file, like this:
exports.onRouteUpdate = () => {
window.rudderanalytics && window.rudderanalytics.page();
};
Note: The above code snippet might not give the best results all the time as it doesn't take various SDK loading scenarios into consideration. Hence, if you are not tracking page views automatically, then clone the contents of the gatsby-browser.js
from the package and make necessary modifications.
You’ve now successfully installed rudder-analytics.js
tracking. You can enable and use any event destination to send your event data via RudderStack, in no time at all!
There are cases when you may want to tap into the features provide by end destination SDKs to enhance tracking and other functionalities. RudderStack's JavaScript SDK exposes a ready
API with a callback
parameter, that fires when the SDK is done initializing itself and other third-party native SDK destinations.
For example:
window.rudderanalytics.ready(() => {
console.log('we are all set!!!');
});
For detailed technical documentation and troubleshooting guide on the RudderStack’s JavaScript SDK, check out our docs. |
---|
RudderStack's Querystring API allows you to trigger track
, identify
calls using query parameters. If you pass the following parameters in the URL, then it will trigger the corresponding SDK API call.
For example:
https://hostname.com/?ajs_uid=12345&ajs_event=test%20event&ajs_aid=abcde&ajs_prop_testProp=prop1&ajs_trait_name=Firstname+Lastname
For the above URL, the below SDK calls will be triggered:
rudderanalytics.identify('12345', { name: 'Firstname Lastname' });
rudderanalytics.track('test event', { testProp: 'prop1' });
rudderanalytics.setAnonymousId('abcde');
You may use the below parameters as querystring parameter and trigger the corresponding call.
ajs_uid
: Makes a rudderanalytics.identify()
call with userId
having the value of the parameter value.
ajs_aid
: Makes a rudderanalytics.setAnonymousId()
call with anonymousId
having the value of the parameter value.
ajs_event
: Makes a rudderanalytics.track()
call with event
name as parameter value.
ajs_prop_<property>
: If ajs_event
is passed as querystring, value of this parameter will populate the properties
of the corresponding event in the track
call.
ajs_trait_<trait>
: If ajs_uid
is provided as querysting, value of this parameter will populate the traits
of the identify
call made.
You can also define callbacks to the common methods of the rudderanalytics
object.
Note: For now, the functionality is supported for syncPixel
method which is called in the SDK when making sync calls in integrations for relevant destinations.
For example, you can load the rudderanalytics with callbacks in your Gatsby browser file:
window.rudderanalytics.syncPixelCallback = (obj) => {
window.rudderanalytics.track(
'sync lotame',
{ destination: obj.destination },
{ integrations: { All: false, S3: true } },
);
};
Note: in order for this to work well, you must also set the manualLoad option to true in your Gatsby configuration, and load rudderstack in the browser gatsby file manually.
In the above example, we are defining a syncPixelCallback
on the analytics object before the call to load the SDK. This will lead to calling of this registered callback with the parameter {destination: <destination_name>}
whenever a sync call is made from the SDK for relevant integrations like Lotame.
The callback can be supplied in options parameter like below as well:
rudderanalytics.load(YOUR_WRITE_KEY, DATA_PLANE_URL, {
clientSuppliedCallbacks: {
syncPixelCallback: () => {
console.log('sync done!');
},
},
});
License
RudderStack gatsby-plugin-rudderstack is released under the AGPLv3 License.
Contribute
We would love to see you contribute to RudderStack. Get more information on how to contribute here.
Follow Us
:clap: Our Supporters