Skip to content
Merged
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
9 changes: 9 additions & 0 deletions integrations/profitwell/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"parserOptions": {
"ecmaVersion": 6
},
"env": {
"browser": true
},
"extends": "@segment/eslint-config/browser/legacy"
}
3 changes: 3 additions & 0 deletions integrations/profitwell/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
node_modules
.idea
4 changes: 4 additions & 0 deletions integrations/profitwell/HISTORY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1.0.0 / 2019-05-10
==================

* First stab at Profitwell DotJS
7 changes: 7 additions & 0 deletions integrations/profitwell/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) 2019 Chris Sperandio, Segment chris.sperandio@segment.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
80 changes: 80 additions & 0 deletions integrations/profitwell/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
##
# Binaries
##

ESLINT := node_modules/.bin/eslint
KARMA := node_modules/.bin/karma

##
# Files
##

LIBS = $(shell find lib -type f -name "*.js")
TESTS = $(shell find test -type f -name "*.test.js")
SUPPORT = $(wildcard karma.conf*.js)
ALL_FILES = $(LIBS) $(TESTS) $(SUPPORT)

##
# Program options/flags
##

# A list of options to pass to Karma
# Overriding this overwrites all options specified in this file (e.g. BROWSERS)
KARMA_FLAGS ?=

# A list of Karma browser launchers to run
# http://karma-runner.github.io/0.13/config/browsers.html
BROWSERS ?=
ifdef BROWSERS
KARMA_FLAGS += --browsers $(BROWSERS)
endif

ifdef CI
KARMA_CONF ?= karma.conf.ci.js
else
KARMA_CONF ?= karma.conf.js
endif

# Mocha flags.
GREP ?= .

##
# Tasks
##

# Install node modules.
node_modules: package.json $(wildcard node_modules/*/package.json)
@npm install
@touch $@

# Install dependencies.
install: node_modules

# Remove temporary files and build artifacts.
clean:
rm -rf *.log coverage
.PHONY: clean

# Remove temporary files, build artifacts, and vendor dependencies.
distclean: clean
rm -rf node_modules
.PHONY: distclean

# Lint JavaScript source files.
lint: install
@$(ESLINT) $(ALL_FILES)
.PHONY: lint

# Attempt to fix linting errors.
fmt: install
@$(ESLINT) --fix $(ALL_FILES)
.PHONY: fmt

# Run browser unit tests in a browser.
test-browser: install
@$(KARMA) start $(KARMA_FLAGS) $(KARMA_CONF)

# Default test target.
test: lint test-browser
.PHONY: test
.DEFAULT_GOAL = test
10 changes: 10 additions & 0 deletions integrations/profitwell/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# analytics.js-integration-profitwell-dotjs

Profitwell DotJS integration for [Analytics.js][].

## License

Released under the [MIT license](LICENSE).

[Analytics.js]: https://segment.com/docs/libraries/analytics.js/

1 change: 1 addition & 0 deletions integrations/profitwell/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/profitwell/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../karma.conf');
52 changes: 52 additions & 0 deletions integrations/profitwell/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

var integration = require('@segment/analytics.js-integration');
var Identify = require('segmentio-facade').Identify;

var ProfitWell = module.exports = integration('ProfitWell')
.global('profitwell')
.option('publicApiToken')
.option('siteType', 'marketing')
.tag('<script src="https://public.profitwell.com/js/profitwell.js?auth={{publicApiToken}}">');

ProfitWell.prototype.initialize = function() {
window.profitwell = window.profitwell || function() {
window.profitwell.q = window.profitwell.q || [];
window.profitwell.q.push(arguments);
};

var user = this.analytics.user();
var traits = user.traits() || {};
var id = new Identify({ traits: traits });
var email = id.email();

window.profitwell('auth_token', this.options.publicApiToken);

if (email) {
this.start(email);
}

if (this.options.siteType === 'marketing') {
this.start();
}

this.load(this.ready);
};

ProfitWell.prototype.identify = function(identify) {
if (identify.email()) {
this.start(identify.email());
} else {
this.start();
}
};

ProfitWell.prototype.start = function(email) {
if (this.started) {
return;
}

var args = email ? { user_email: email } : {};
window.profitwell('start', args);
this.started = true;
};
55 changes: 55 additions & 0 deletions integrations/profitwell/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@profitwell/analytics.js-integration",
"description": "The Profitwell DotJS analytics.js integration.",
"version": "1.0.1",
"keywords": [
"analytics.js",
"analytics.js-integration",
"segment",
"profitwell"
],
"main": "lib/index.js",
"scripts": {
"test": "karma start",
"test:ci": "karma start karma.conf-ci.js"
},
"repository": {
"type": "git",
"url": "https://github.com/ProfitWell/segment-dotjs"
},
"author": "",
"license": "SEE LICENSE IN LICENSE",
"bugs": {
"url": "https://github.com/ProfitWell/segment-dotjs/issues"
},
"homepage": "https://github.com/ProfitWell/segment-dotjs#readme",
"dependencies": {
"@segment/analytics.js-integration": "^3.1.0",
"segmentio-facade": "^3.1.0"
},
"devDependencies": {
"@segment/analytics.js-core": "^3.0.0",
"@segment/analytics.js-integration-tester": "^3.1.0",
"@segment/clear-env": "^2.0.0",
"@segment/eslint-config": "^3.1.1",
"browserify": "^13.0.0",
"browserify-istanbul": "^2.0.0",
"eslint": "^2.9.0",
"eslint-plugin-mocha": "^2.2.0",
"eslint-plugin-require-path-exists": "^1.1.5",
"istanbul": "^0.4.3",
"karma": "1.3.0",
"karma-browserify": "^5.0.4",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.0.0",
"karma-junit-reporter": "^1.0.0",
"karma-mocha": "1.0.1",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sauce-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.26",
"mocha": "^2.2.5",
"npm-check": "^5.2.1",
"phantomjs-prebuilt": "^2.1.7",
"watchify": "^3.7.0"
}
}
3 changes: 3 additions & 0 deletions integrations/profitwell/test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@segment/eslint-config/mocha"
}
153 changes: 153 additions & 0 deletions integrations/profitwell/test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
'use strict';

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

describe('Profitwell', function() {
var analytics;
var profitwell;
var options = {
publicApiToken: '123123123'
};
var testEmail = 'testEmail@pw.com';
var testToken = 'test_token';

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

afterEach(function(done) {
analytics.waitForScripts(function() {
analytics.restore();
analytics.reset();
profitwell.reset();
sandbox();
done();
});
});

describe('before loading', function() {
describe('#initialize', function() {
beforeEach(function() {
analytics.stub(profitwell, 'load');
analytics.stub(profitwell, 'start');
analytics.stub(window, 'profitwell');
});

it('should call load on initialize', function() {
analytics.initialize();

analytics.called(profitwell.load);
});

it('should create profitwell object', function() {
analytics.initialize();

analytics.assert(window.profitwell instanceof Function);
});

it('should call auth_token with the token', () => {
profitwell.options.publicApiToken = testToken;

analytics.initialize();

analytics.called(window.profitwell, 'auth_token', testToken);
});

it('should call start with email on initialize', function() {
addTraitsToStorage();
analytics.initialize();

analytics.called(profitwell.start, testEmail);
});

it('should call start with nothing on anonymous initialize', function() {
profitwell.options.siteType = 'marketing';
analytics.initialize();

analytics.called(profitwell.start);
});
});

describe('#start', function() {
beforeEach(function() {
analytics.stub(window, 'profitwell');
});

it('should call start with user\'s email', function() {
profitwell.start(testEmail);

analytics.called(window.profitwell, 'start', { user_email: testEmail });
});

it('should call start with an empty object', function() {
profitwell.start();

analytics.called(window.profitwell, 'start', {});
});

it('should not call start more than once', function() {
profitwell.start(testEmail);
profitwell.start('another@pw.com');

analytics.called(window.profitwell, 'start', { user_email: testEmail });
});

it('should not call start more than once when initialized with email', function(done) {
addTraitsToStorage();
analytics.initialize();

analytics.called(window.profitwell, 'start', { user_email: testEmail });

analytics.once('ready', function() {
try {
analytics.stub(window, 'profitwell');
analytics.identify(testEmail);

analytics.didNotCall(window.profitwell, 'start');
done();
} catch (ex) {
done(ex);
}
});
});
});
});

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

describe('#identify', function() {
it('should call start with the user\'s email', function() {
analytics.identify(testEmail);
analytics.called(profitwell.start, testEmail);
});

it('should call start with an empty object', function() {
analytics.identify();
analytics.called(profitwell.start);
});
});
});

function addTraitsToStorage() {
// this is a hack to get analytics to initialize a user with an email
localStorage.setItem(
'ajs_user_traits',
// eslint-disable-next-line no-restricted-globals
JSON.stringify({ email: testEmail })
);
}
});