Skip to content

Commit f52288a

Browse files
authored
feat: Added Fbclid config option (#338)
* Added Fbclid config option * removed testing api key * added new line at the end of test/amplitude-client.js * added break line between functions in amplitude-client.js * fixed lint issues
1 parent cc2c941 commit f52288a

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

src/amplitude-client.js

+19
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ AmplitudeClient.prototype._trackParamsAndReferrer = function _trackParamsAndRefe
393393
let utmProperties;
394394
let referrerProperties;
395395
let gclidProperties;
396+
let fbclidProperties;
396397
if (this.options.includeUtm) {
397398
utmProperties = this._initUtmData();
398399
}
@@ -402,11 +403,15 @@ AmplitudeClient.prototype._trackParamsAndReferrer = function _trackParamsAndRefe
402403
if (this.options.includeGclid) {
403404
gclidProperties = this._saveGclid(this._getUrlParams());
404405
}
406+
if (this.options.includeFbclid) {
407+
fbclidProperties = this._saveFbclid(this._getUrlParams());
408+
}
405409
if (this.options.logAttributionCapturedEvent) {
406410
const attributionProperties = {
407411
...utmProperties,
408412
...referrerProperties,
409413
...gclidProperties,
414+
...fbclidProperties,
410415
};
411416
if (Object.keys(attributionProperties).length > 0) {
412417
this.logEvent(Constants.ATTRIBUTION_EVENT, attributionProperties);
@@ -801,6 +806,20 @@ AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) {
801806
return gclidProperties;
802807
};
803808

809+
/**
810+
* Try to fetch Facebook Fbclid from url params.
811+
* @private
812+
*/
813+
AmplitudeClient.prototype._saveFbclid = function _saveFbclid(urlParams) {
814+
var fbclid = utils.getQueryParam('fbclid', urlParams);
815+
if (utils.isEmptyString(fbclid)) {
816+
return;
817+
}
818+
var fbclidProperties = { fbclid: fbclid };
819+
_sendParamsReferrerUserProperties(this, fbclidProperties);
820+
return fbclidProperties;
821+
};
822+
804823
/**
805824
* Try to fetch Amplitude device id from url params.
806825
* @private

src/options.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ if (BUILD_COMPAT_REACT_NATIVE) {
2929
* @property {number} [eventUploadPeriodMillis=`30000` (30 sec)] - Amount of time in milliseconds that the SDK waits before uploading events if batchEvents is true.
3030
* @property {number} [eventUploadThreshold=`30`] - Minimum number of events to batch together per request if batchEvents is true.
3131
* @property {boolean} [forceHttps=`true`] - If `true`, the events will always be uploaded to HTTPS endpoint. Otherwise, it will use the embedding site's protocol.
32+
* @property {boolean} [includeFbclid=`false`] - If `true`, captures the fbclid URL parameter as well as the user's initial_fbclid via a setOnce operation.
3233
* @property {boolean} [includeGclid=`false`] - If `true`, captures the gclid URL parameter as well as the user's initial_gclid via a setOnce operation.
3334
* @property {boolean} [includeReferrer=`false`] - If `true`, captures the referrer and referring_domain for each session, as well as the user's initial_referrer and initial_referring_domain via a setOnce operation.
3435
* @property {boolean} [includeUtm=`false`] - If `true`, finds UTM parameters in the query string or the _utmz cookie, parses, and includes them as user properties on all events uploaded. This also captures initial UTM parameters for each session via a setOnce operation.
3536
* @property {string} [language=The language determined by the browser] - Custom language to set.
3637
* @property {string} [logLevel=`WARN`] - Level of logs to be printed in the developer console. Valid values are 'DISABLE', 'ERROR', 'WARN', 'INFO'. To learn more about the different options, see below.
37-
* @property {boolean} [logAttributionCapturedEvent=`false`] - If `true`, the SDK will log an Amplitude event anytime new attribution values are captured from the user. **Note: These events count towards your event volume.** Event name being logged: [Amplitude] Attribution Captured. Event Properties that can be logged: `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`, `referrer`, `referring_domain`, `gclid`. For UTM properties to be logged, `includeUtm` must be set to `true`. For the `referrer` and `referring_domain` properties to be logged, `includeReferrer` must be set to `true`. For the `gclid` property to be logged, `includeGclid` must be set to `true`.
38+
* @property {boolean} [logAttributionCapturedEvent=`false`] - If `true`, the SDK will log an Amplitude event anytime new attribution values are captured from the user. **Note: These events count towards your event volume.** Event name being logged: [Amplitude] Attribution Captured. Event Properties that can be logged: `utm_source`, `utm_medium`, `utm_campaign`, `utm_term`, `utm_content`, `referrer`, `referring_domain`, `gclid`, `fbclid`. For UTM properties to be logged, `includeUtm` must be set to `true`. For the `referrer` and `referring_domain` properties to be logged, `includeReferrer` must be set to `true`. For the `gclid` property to be logged, `includeGclid` must be set to `true`. For the `fbclid` property to be logged, `includeFbclid` must be set to `true`.
3839
* @property {boolean} [optOut=`false`] - Whether or not to disable tracking for the current user.
3940
* @property {function} [onError=`() => {}`] - Function to call on error.
4041
* @property {string} [platform=`Web`|`iOS`|`Android`] - Platform device is running on. `Web` is a browser (including mobile browsers). `iOS` and `Android` are relevant only for react-native apps.
4142
* @property {number} [savedMaxCount=`1000`] - Maximum number of events to save in localStorage. If more events are logged while offline, then old events are removed.
4243
* @property {boolean} [saveEvents=`true`] - If `true`, saves events to localStorage and removes them upon successful upload. *Note: Without saving events, events may be lost if the user navigates to another page before the events are uploaded.*
43-
* @property {boolean} [saveParamsReferrerOncePerSession=`true`] - If `true`, then includeGclid, includeReferrer, and includeUtm will only track their respective properties once per session. New values that come in during the middle of the user's session will be ignored. Set to false to always capture new values.
44+
* @property {boolean} [saveParamsReferrerOncePerSession=`true`] - If `true`, then includeGclid, includeFbclid, includeReferrer, and includeUtm will only track their respective properties once per session. New values that come in during the middle of the user's session will be ignored. Set to false to always capture new values.
4445
* @property {boolean} [secureCookie=`false`] - If `true`, the amplitude cookie will be set with the Secure flag.
4546
* @property {number} [sessionTimeout=`30*60*1000` (30 min)] - The time between logged events before a new session starts in milliseconds.
4647
* @property {Object} [trackingOptions=`{ city: true, country: true, carrier: true, device_manufacturer: true, device_model: true, dma: true, ip_address: true, language: true, os_name: true, os_version: true, platform: true, region: true, version_name: true}`] - Type of data associated with a user.
@@ -63,6 +64,7 @@ export default {
6364
eventUploadPeriodMillis: 30 * 1000, // 30s
6465
eventUploadThreshold: 30,
6566
forceHttps: true,
67+
includeFbclid: false,
6668
includeGclid: false,
6769
includeReferrer: false,
6870
includeUtm: false,

test/amplitude-client.js

+83
Original file line numberDiff line numberDiff line change
@@ -3319,6 +3319,89 @@ describe('AmplitudeClient', function () {
33193319
});
33203320
});
33213321

3322+
describe('gatherFbclid', function () {
3323+
var clock;
3324+
beforeEach(function () {
3325+
clock = sinon.useFakeTimers(100);
3326+
amplitude.init(apiKey);
3327+
sinon.stub(amplitude, '_getUrlParams').returns('?utm_source=amplitude&utm_medium=email&fbclid=67890&gclid=12345');
3328+
});
3329+
3330+
afterEach(function () {
3331+
reset();
3332+
amplitude._getUrlParams.restore();
3333+
clock.restore();
3334+
});
3335+
3336+
it('should parse fbclid once per session', function () {
3337+
amplitude.init(apiKey, undefined, { includeFbclid: true });
3338+
3339+
// still same session, no fbclid sent
3340+
assert.lengthOf(server.requests, 0);
3341+
assert.lengthOf(amplitude._unsentEvents, 0);
3342+
assert.lengthOf(amplitude._unsentIdentifys, 0);
3343+
3344+
// advance the clock to force a new session
3345+
clock.tick(30 * 60 * 1000 + 1);
3346+
amplitude.init(apiKey, undefined, { includeFbclid: true, batchEvents: true, eventUploadThreshold: 2 });
3347+
amplitude.logEvent('Fbclid test event', {});
3348+
assert.lengthOf(server.requests, 1);
3349+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3350+
assert.lengthOf(events, 2);
3351+
3352+
// first event should be identify with Fbclid
3353+
assert.equal(events[0].event_type, '$identify');
3354+
assert.deepEqual(events[0].user_properties, {
3355+
$set: { fbclid: '67890' },
3356+
$setOnce: { initial_fbclid: '67890' },
3357+
});
3358+
3359+
// second event should be the test event with no fbclid information
3360+
assert.equal(events[1].event_type, 'Fbclid test event');
3361+
assert.deepEqual(events[1].user_properties, {});
3362+
});
3363+
3364+
it('should parse fbclid multiple times per session if configured', function () {
3365+
amplitude.init(apiKey, undefined, { includeFbclid: true, saveParamsReferrerOncePerSession: false });
3366+
3367+
// even though session is same, fbclid is sent again
3368+
assert.lengthOf(server.requests, 1);
3369+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3370+
assert.lengthOf(events, 1);
3371+
assert.equal(events[0].event_type, '$identify');
3372+
assert.deepEqual(events[0].user_properties, {
3373+
$set: { fbclid: '67890' },
3374+
$setOnce: { initial_fbclid: '67890' },
3375+
});
3376+
});
3377+
3378+
it('should log attribution event when fbclid is captured if configured', () => {
3379+
clock.tick(30 * 60 * 1000 + 1);
3380+
amplitude.init(apiKey, undefined, {
3381+
includeFbclid: true,
3382+
logAttributionCapturedEvent: true,
3383+
batchEvents: true,
3384+
eventUploadThreshold: 2,
3385+
});
3386+
3387+
assert.lengthOf(server.requests, 1);
3388+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3389+
assert.lengthOf(events, 2);
3390+
3391+
// first event should be identify with fbclid
3392+
assert.equal(events[0].event_type, '$identify');
3393+
assert.deepEqual(events[0].user_properties, {
3394+
$set: { fbclid: '67890' },
3395+
$setOnce: { initial_fbclid: '67890' },
3396+
});
3397+
// second event should be the attribution captured event with fbclid populated
3398+
assert.equal(events[1].event_type, constants.ATTRIBUTION_EVENT);
3399+
assert.deepEqual(events[1].event_properties, {
3400+
fbclid: '67890',
3401+
});
3402+
});
3403+
});
3404+
33223405
describe('logRevenue', function () {
33233406
beforeEach(function () {
33243407
amplitude.init(apiKey);

0 commit comments

Comments
 (0)