Skip to content

Commit

Permalink
Add Angulartics waiting for vendor support
Browse files Browse the repository at this point in the history
  • Loading branch information
hassanbazzi committed Aug 1, 2016
1 parent e085554 commit 499f021
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 59 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "angulartics-segment",
"main": "./dist/angulartics-segment.min.js",
"version": "0.2.0",
"dependencies": {
"angulartics": "^1.0.2"
"angulartics": "^1.1.0"
},
"ignore": [
"**/.*",
Expand Down
118 changes: 62 additions & 56 deletions lib/angulartics-segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,71 +8,77 @@
angular.module('angulartics.segment', ['angulartics'])
.config(['$analyticsProvider', function ($analyticsProvider) {

// https://segment.com/docs/libraries/analytics.js/#page
// analytics.page([category], [name], [properties], [options], [callback]);
// TODO : Support optional parameters where the parameter order and type changes their meaning
// e.g.
// (string) is (name)
// (string, string) is (category, name)
// (string, object) is (name, properties)
$analyticsProvider.registerPageTrack(function (path, properties) {
try {
// only pass `path`, segment script will automatically populate properties
analytics.page(path);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
// This allows users to load their segment library async, queue events,
// and send them whenever the script is ready
angulartics.waitForVendorApi("analytics", 1000, "initialized", registerHandlers);

function registerHandlers(){
// https://segment.com/docs/libraries/analytics.js/#page
// analytics.page([category], [name], [properties], [options], [callback]);
// TODO : Support optional parameters where the parameter order and type changes their meaning
// e.g.
// (string) is (name)
// (string, string) is (category, name)
// (string, object) is (name, properties)
$analyticsProvider.registerPageTrack(function (path, properties) {
try {
// only pass `path`, segment script will automatically populate properties
analytics.page(path);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
});
});

// https://segment.com/docs/libraries/analytics.js/#track
// analytics.track(event, [properties], [options], [callback]);
$analyticsProvider.registerEventTrack(function (event, properties, options, callback) {
try {
analytics.track(event, properties, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
// https://segment.com/docs/libraries/analytics.js/#track
// analytics.track(event, [properties], [options], [callback]);
$analyticsProvider.registerEventTrack(function (event, properties, options, callback) {
try {
analytics.track(event, properties, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
});
});

// https://segment.com/docs/libraries/analytics.js/#identify
// analytics.identify([userId], [traits], [options], [callback]);
$analyticsProvider.registerSetUserProperties(function (userId, traits, options, callback) {
try {
analytics.identify(userId, traits, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
// https://segment.com/docs/libraries/analytics.js/#identify
// analytics.identify([userId], [traits], [options], [callback]);
$analyticsProvider.registerSetUserProperties(function (userId, traits, options, callback) {
try {
analytics.identify(userId, traits, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
});
});

// https://segment.com/docs/libraries/analytics.js/#identify
// analytics.identify([userId], [traits], [options], [callback]);
$analyticsProvider.registerSetUserPropertiesOnce(function (userId, traits, options, callback) {
try {
analytics.identify(userId, traits, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
// https://segment.com/docs/libraries/analytics.js/#identify
// analytics.identify([userId], [traits], [options], [callback]);
$analyticsProvider.registerSetUserPropertiesOnce(function (userId, traits, options, callback) {
try {
analytics.identify(userId, traits, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
});
});

// https://segment.com/docs/libraries/analytics.js/#alias
// analytics.alias(userId, previousId, options, callback);
$analyticsProvider.registerSetAlias(function (userId, previousId, options, callback) {
try {
analytics.alias(userId, previousId, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
// https://segment.com/docs/libraries/analytics.js/#alias
// analytics.alias(userId, previousId, options, callback);
$analyticsProvider.registerSetAlias(function (userId, previousId, options, callback) {
try {
analytics.alias(userId, previousId, options, callback);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
});
});
}

}]);

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angulartics-segment",
"version": "0.1.5",
"version": "0.2.0",
"description": "Segment plugin for Angulartics",
"keywords": [
"segment",
Expand Down Expand Up @@ -32,7 +32,7 @@
},
"license": "MIT",
"peerDependencies": {
"angulartics": "^1.0.0"
"angulartics": "^1.1.0"
},
"devDependencies": {
"uglify-js": "^2.4.23"
Expand Down

0 comments on commit 499f021

Please sign in to comment.