Last active
December 4, 2017 13:28
-
-
Save sean-hill/627fa40f96577baae378 to your computer and use it in GitHub Desktop.
Branch Metrics Service for AngularJS / Ionic project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); | |
}) | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Branch Metrics Service for AngularJS / Ionic project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
}; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}); | |
}; | |
}) |
seems like branch changed their API since this gist, e.g. branch.getInstance
and branch.initSession
no longer exist.
Go here for Cordova/Ionic docs: https://dev.branch.io/references/cordova_phonegap_sdk/
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
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?