Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ReplayBird Integration #755

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions integrations/replaybird/HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1.0.0 / 2020-02-26
==================

* Initial commit
12 changes: 12 additions & 0 deletions integrations/replaybird/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# analytics.js-integration-replaybird [![Build Status][ci-badge]][ci-link]

ReplayBird integration for [Analytics.js][].

## License

Released under the [MIT license](LICENSE).


[Analytics.js]: https://segment.com/docs/libraries/analytics.js/
[ci-link]: https://circleci.com/gh/segment-integrations/analytics.js-integration-replaybird
[ci-badge]: https://circleci.com/gh/segment-integrations/analytics.js-integration-replaybird.svg?style=svg
1 change: 1 addition & 0 deletions integrations/replaybird/karma.conf-ci.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../karma.conf-ci.js');
1 change: 1 addition & 0 deletions integrations/replaybird/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../karma.conf');
118 changes: 118 additions & 0 deletions integrations/replaybird/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
'use strict';

/**
* Module dependencies.
*/

var camel = require('camelcase');
var foldl = require('@ndhoule/foldl');
var integration = require('@segment/analytics.js-integration');

/**
* Expose `ReplayBird` integration.
*
* https://www.replaybird.com
*/

var ReplayBird = (module.exports = integration('ReplayBird')
.option('siteKey', '')
.option('debug', false));

/**
* Initialize.
*/
ReplayBird.prototype.initialize = function() {

/* eslint-disable no-param-reassign */
!function(t,e){var o,n,p,r;e.__SV||(window.replaybird=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src="https://cdn.replaybird.com/agent/latest/replaybird.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="replaybird",u.people=u.people||[],u.toString=function(t){var e="replaybird";return"replaybird"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="identify capture alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.replaybird||[]);
/* eslint-enable no-param-reassign */

window.replaybird.init(this.options.siteKey, { });
// if (this.options.debug) {
// window.replaybird.debug();
// }
this.ready();
};

/**
* Loaded?
*
* @return {Boolean}
*/
ReplayBird.prototype.loaded = function() {
return !!window.replaybird;
};

/**
* Identify
*
* @param {Identify} identify
*/
ReplayBird.prototype.identify = function(identify) {
if (!identify.userId()) {
return this.debug('User id is required!');
}

var traits = identify.traits({ name: 'displayName' });

var newTraits = foldl(
function(results, value, key) {
var rs = results;
if (key !== 'id') {
rs[
key === 'displayName' || key === 'email' ? key : camelCaseField(key)
] = value;
}
return rs;
},
{},
traits
);

var id = String(identify.userId());
window.replaybird.identify(id, newTraits);
};

/**
* Track. Passes the events directly to ReplayBird
*
* @param {Track} track
*/
ReplayBird.prototype.track = function(track) {
if (window.replaybird) {
window.replaybird.capture(track.event(), track.properties());
}
};

/**
* Camel cases `.`, `-`, `_`, and white space within fieldNames. Leaves type suffix alone.
*
* NOTE: Does not fix otherwise malformed fieldNames.
* ReplayBird will scrub characters from keys that do not conform to /^[a-zA-Z][a-zA-Z0-9_]*$/.
*
* @param {string} fieldName
*/
function camelCaseField(fieldName) {
// Do not camel case across type suffixes.
var parts = fieldName.split('_');
if (parts.length > 1) {
var typeSuffix = parts.pop();
switch (typeSuffix) {
case 'str':
case 'int':
case 'date':
case 'real':
case 'bool':
case 'strs':
case 'ints':
case 'dates':
case 'reals':
case 'bools':
return camel(parts.join('_')) + '_' + typeSuffix;
default: // passthrough
}
}

// No type suffix found. Camel case the whole field name.
return camel(fieldName);
}
49 changes: 49 additions & 0 deletions integrations/replaybird/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "@segment/analytics.js-integration-replaybird",
"description": "The ReplayBird analytics.js integration.",
"version": "1.0.0",
"keywords": [
"analytics.js",
"analytics.js-integration",
"segment",
"replaybird"
],
"main": "lib/index.js",
"scripts": {
"test": "karma start",
"test:ci": "karma start karma.conf-ci.js"
},
"author": "Segment <[email protected]>",
"license": "SEE LICENSE IN LICENSE",
"homepage": "https://github.com/segmentio/analytics.js-integrations/blob/master/integrations/replaybird#readme",
"bugs": {
"url": "https://github.com/segmentio/analytics.js-integrations/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/segmentio/analytics.js-integrations.git"
},
"dependencies": {
"@ndhoule/foldl": "^2.0.1",
"@segment/analytics.js-integration": "^2.1.0",
"camelcase": "^3.0.0",
"is": "^3.1.0"
},
"devDependencies": {
"@segment/analytics.js-core": "^3.8.2",
"@segment/analytics.js-integration-tester": "^3.1.1",
"@segment/clear-env": "^2.1.1",
"browserify": "^16.2.3",
"eslint": "^5.16.0",
"karma": "^4.1.0",
"karma-browserify": "^6.0.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"karma-sauce-launcher": "^2.0.2",
"karma-spec-reporter": "^0.0.32",
"karma-summary-reporter": "^1.6.0",
"mocha": "^6.1.4",
"watchify": "^3.11.1"
}
}
116 changes: 116 additions & 0 deletions integrations/replaybird/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict';

var Analytics = require('@segment/analytics.js-core').constructor;
var integration = require('@segment/analytics.js-integration');
var sandbox = require('@segment/clear-env');
var tester = require('@segment/analytics.js-integration-tester');
var ReplayBird = require('../lib/');

describe('ReplayBird', function() {
var analytics;
var replaybird;
var options = {
siteKey: '7vrdFkeywUwS94YQXUTdBf0pPQHXNlcWihmo1gsKLBO',
debug: false
};

beforeEach(function() {
analytics = new Analytics();
replaybird = new ReplayBird(options);
analytics.use(ReplayBird);
analytics.use(tester);
analytics.add(replaybird);
});

afterEach(function() {
analytics.restore();
analytics.reset();
replaybird.reset();
sandbox();
});

it('should have the right settings', function() {
analytics.compare(
ReplayBird,
integration('ReplayBird')
.option('siteKey', '')
.option('debug', false)
);
});

describe('before loading', function() {
beforeEach(function() {
analytics.stub(replaybird, 'load');
});

describe('#initialize', function() {
it('should call #load', function() {
analytics.initialize();
analytics.called(replaybird.load);
});
});
});

describe('after loading', function() {
beforeEach(function(done) {
analytics.once('ready', done);
analytics.initialize();
analytics.page();
});

describe('#identify', function() {
beforeEach(function() {
analytics.stub(window.ReplayBird, 'identify');
});

it('should send an id', function() {
analytics.identify('id');
analytics.called(window.ReplayBird.identify, 'id', {});
});

it('should camel case custom props', function() {
analytics.identify('id', {
name: 'User123',
email: '[email protected]',
'First name': 'Eric',
lastName: 'Brown'
});
analytics.called(
window.ReplayBird.identify,
'id',
{
name: 'User123',
email: '[email protected]',
firstName: 'Eric',
lastName: 'Brown'
}
);
});

it('should map name and email', function() {
analytics.identify('id', { name: 'Test', email: '[email protected]' });
analytics.called(
window.ReplayBird.identify,
'id',
{ name: 'Test', email: '[email protected]' }
);
});
});

describe('#track', function() {
beforeEach(function() {
analytics.stub(window.ReplayBird, 'event');
});

it('should send track event name and properties', function() {
analytics.track('my_event', { some_field: 'field_value' });
analytics.called(
window.ReplayBird.event,
'my_event',
{ some_field: 'field_value' }
);
});
});

});
});