Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sean-hill/627fa40f96577baae378 to your computer and use it in GitHub Desktop.
Save sean-hill/627fa40f96577baae378 to your computer and use it in GitHub Desktop.
Branch Metrics Service for AngularJS / Ionic project
// Your App
angular.module('your-app', ['ionic'])
.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
$stateProvider
.state("something-cool", {
url: "/something-cool",
controller: "SomethingCoolCtrl",
templateUrl: "templates/something-cool.html",
resolve: {
branchParams: function($q, Branch) {
var deferred = $q.defer();
Branch.getInstance().then(function(){
Branch.initSession().then(function(branchParams) {
deferred.resolve(branchParams);
});
});
return deferred.promise;
}
}
})
// Fallback to this route
$urlRouterProvider.otherwise("/something-cool");
})
;
Branch Metrics Service for AngularJS / Ionic project
// Branch IO docs found here:
// https://github.com/BranchMetrics/Branch-PhoneGap-Cordova-SDK
.factory('Branch', function ($q, $ionicPlatform) {
return {
getInstance: function() {
var deferred = $q.defer();
$ionicPlatform.ready(function(){
var branch = window.Branch;
branch.getInstance('<BRANCH_IO_TOKEN>', function() {
deferred.resolve();
});
});
return deferred.promise;
},
initSession: function() {
var deferred = $q.defer();
$ionicPlatform.ready(function(){
var branch = window.Branch;
branch.initSession(true, function(params) {
deferred.resolve(params);
});
});
return deferred.promise;
},
getShortUrl: function(params, channel, feature) {
var deferred = $q.defer();
var branch = window.Branch;
branch.getShortUrl(params || {}, channel, feature, function(url) {
deferred.resolve(url);
});
return deferred.promise;
},
setIdentity: function(identity) {
var deferred = $q.defer();
var branch = window.Branch;
branch.setIdentity(identity, function(){
deferred.resolve();
});
return deferred.promise;
}
};
})
// Something Cool Ctrl
.controller('SomethingCoolCtrl', function($scope, branchParams, Branch) {
$scope.$on('$ionicView.beforeEnter', function() {
if (branchParams) {
console.log(branchParams)
}
});
$scope.setBranchIdentity = function() {
Branch.setIdentity('Cool dude').then(function(){
// Set identity
});
};
$scope.getBranchShortUrl = function() {
Branch.getShortUrl({ meta: 'data' }).then(function(url){
// Url:
// https://bnc.lt/l/3EnFcu--Cx
});
};
})
@keithdmoore
Copy link

Were you able to get branch.io to work on both Android and iOS? Would you recommend using branch.io for deep linking and deferred deep linking?

@yossi-shasho
Copy link

seems like branch changed their API since this gist, e.g. branch.getInstance and branch.initSession no longer exist.

@keithdmoore
Copy link

@amrayoub
Copy link

amrayoub commented Dec 4, 2017

can you do the same for ionic 3 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment