Updating Lead Info in marketo - marketo

Initially, I associate the lead when the user signs up to our app on a 14 days trial period.
function associateMarketoLead() {
if (window.marketoKey)) {
if (typeof Munchkin !== 'undefined') {
if ('function' === typeof Munchkin.munchkinFunction) {
let leadAttributes = {
Email: user.email,
accountId: accountId,
LeadSource: 'Web'
};
Munchkin.munchkinFunction('associateLead', leadAttributes, marketoKey);
}
}
}
}
We use marketo to send email campaigns at the end of 14-days trial. But, for certain leads, we extend the trial period and so we want to update the lead's database with trial extended date. How can I do that ? I tried the following, but it doesn't work
function notifyMarketoOnTrialExtension(accountId, trialExtendedDate) {
if (window.marketoKey) {
if (typeof Munchkin !== 'undefined') {
if ('function' === typeof Munchkin.munchkinFunction) {
var leadAttributes = {
Email: user.email,
accountId: accountId,
trialExtendedDate: trialExtendedDate
};
Munchkin.munchkinFunction('associateLead', leadAttributes, window.marketoKey);
}
}
}
}
Any suggestions ?

This small snippet shows you how to invoke associateLead with the correct key (third parameter), which is an SHA1 hash of your private key concatenated with the email of the lead you are trying to associate. In your example you are just sending the private key as third parameter.
I used this library for calculating the SHA1 hash //cdnjs.cloudflare.com/ajax/libs/jsSHA/2.0.2/sha1.js
var email = 'user#domain.com';
var shaObj = new jsSHA('SHA-1', 'TEXT');
shaObj.update('<your-munchkin-private-key-goes-here>' + email);
var hash = shaObj.getHash('HEX');
Munchkin.init('<your-munchkin-identifier-goes-here>');
Munchkin.munchkinFunction('associateLead', { 'Email': email }, hash);
Hope it helps.
Alejo.

Related

Meteor Accounts.verifyEmail to set role on the user

I would like that every user who verifies his email in my app gets a certain role afterwards. I'm using the alanning:roles package. Anyway, after he clicks on the verification link that I have a function that sets a role in in Mongo for him.
so I found this function but its apparently only client side:
// (client-side)
Template.Homepage.created = function() {
if (Accounts._verifyEmailToken) {
Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) {
if (err != null) {
if (err.message = 'Verify email link expired [403]') {
console.log('Sorry this verification link has expired.')
}
} else {
console.log('Thank you! Your email address has been confirmed.')
}
});
}
};
And I have the method for setting roles on the server side
Accounts.onCreateUser(function (options, user) {
Roles.setRolesOnUserObj(user, ['employer']);
if (options.profile) {
// include the user profile
user.profile = options.profile
}
How can I connect those two, or is there a better way for implementing this logic. I know this is now in the function "onCreateUser" but would put it seperatly if there is a better way.
First, install the matb33:collection-hooks package.
Then you can detect changes to the user's collection on the server:
var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Meteor.users.after.update(function(userId, doc, fieldNames, modifier, options) {
if (indexOf.call(fieldNames, "emails") >= 0 && doc.emails) {
doc.emails.forEach(function(email) {
if (email.verified === true) {
// Verified address - do something....
}
});
}
});

Getting a username from ID without autopublish

I just got done with the rough draft of my app, and thought it was time to remove autopublish and insecure mode. I started transfering all the stray update and insert methods I had been calling on the client to methods. But now I'm having trouble returning a username from an ID.
My function before: (that worked, until I removed autopublish)
challenger: function() {
var postId = Session.get('activePost');
var post = Posts.findOne(postId);
if (post.challenger !== null) {
var challenger = Meteor.users.findOne(post.challenger);
return challenger.username;
}
return false;
}
Now what I'm trying:
Template.lobby.helpers({
challenger: function() {
var postId = Session.get('activePost');
var post = Posts.findOne(postId);
if (post.challenger !== null) {
var userId = post.challenger;
Meteor.call('getUsername', userId, function (err, result) {
if (err) {
console.log(err);
}
return result;
});
}
return false;
},
Using:
Meteor.methods({
getUsername: function(userId) {
var user = Meteor.users.findOne({_id: userId});
var username = user.username;
return username;
},
...
})
I have tried blocking the code, returning values only once they're defined, and console.logging in the call-callback (which returned the correct username to console, but the view remained unchanged)
Hoping someone can find the obvious mistake I'm making, because I've tried for 3 hours now and I can't figure out why the value would be returned in console but not returned to the template.
Helpers need to run synchronously and should not have any side effects. Instead of calling a method to retrieve the user, you should ensure the user(s) you need for that route/template are published. For example your router could wait on subscriptions for both the active post and the post's challenger. Once the client has the necessary documents, you can revert to your original code.

Firebase authentication on App initilisation

This is works:
console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
but this one is not:
$scope.authenticated.currentUser = user.id;
My goal here is to take to take some of the authentication variables (Email+UserID) and then use them to access a profile node ON firebase. On initialization I want the username, email, and a few other things I need for the app.
crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', function ($scope,defautProfileData,$q) {
var dataRef = new Firebase("https://glowing-fire-5401.firebaseIO.com");
$scope.authenticated={
currentUser: $scope.authemail,
emailAddress: "",
settings: "",
};
var chatRef = new Firebase('https://<YOUR-FIREBASE>.firebaseio.com');
var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
if (error) {
// an error occurred while attempting login
switch(error.code) {
case 'INVALID_EMAIL':
case 'INVALID_PASSWORD':
default:
}
} else if (user) {
// user authenticated with Firebase
console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
$scope.authenticated.currentUser = user.id ;//
} else {
// user is logged out
}
});
}]); //GlobaldCtrl
Most likely, you're running into a problem with Angular's HTML Compiler.
Whenever you use an event like ng-click/ng-submit/etc, Angular fires $scope.$apply(), which checks for any changes to your $scope variables and applies them to the DOM.
Since FirebaseSimpleLogin is not part of Angular's purview, it has no idea that when the callback is fired, you've updated $scope.authenticated.currentUser. This would also explain why it works when you call auth.login(), since you're probably invoking that via an ng-click event somewhere, which would fire a digest check and discover the changes.
If this is indeed the case, you can correct this issue by alerting Angular that it needs to run $apply by using $timeout:
crossfitApp.controller('globalIdCtrl', ["$scope",'defautProfileData','$q', '$timeout', function ($scope,defautProfileData,$q, $timeout) {
/* ... */
var auth = new FirebaseSimpleLogin(chatRef, function(error, user) {
if (error) {
/* ... */
} else if (user) {
$timeout(function() {
$scope.authenticated.currentUser = user.id ;//
});
} else {
// user is logged out
}
});

How do you change the email associated with a user in firebase simple login?

As the title suggests I would like to provide functionality to allow a user to update the email they use to login to my app using Firebase Simple Login. Cannot figure out an elegant way to do this. App uses AngularFire if that is relevant.
Does one exist or do I need to create a new account and delete the old one using the $removeUser() and $createUser() methods?
Update for Firebase 2.1.x
The Firebase SDK now provides a changeEmail method.
var ref = new Firebase('https://<instance>.firebaseio.com');
ref.changeEmail({
oldEmail: 'kato#domain.com',
newEmail: 'kato2#kato.com' ,
password: '******'
}, function(err) {
console.log(err ? 'failed to change email: ' + err : 'changed email successfully!');
});
Historical answer for Firebase 1.x
In Simple Login, this is equivalent to changing the user's ID. So there is no way to do this on the fly. Simply create the new account, remove the old one as you have already suggested.
If you're user profiles in Firebase, you'll want to move those as well. Here's brute force, safe method to migrate an account, including user profiles. You could, naturally, improve upon this with some objectification and futures:
var ref = new Firebase('URL/user_profiles');
var auth = new FirebaseSimpleLogin(ref);
// assume user has logged in, or we obtained their old UID by
// looking up email address in our profiles
var oldUid = 'simplelogin:123';
moveUser( auth, ref, oldUid, '123#abc.com', '456#def.com', 'xxx' );
function moveUser( auth, usersRef, oldUid, oldId, newId, pass ) {
// execute activities in order; first we copy old paths to new
createLogin(function(user) {
copyProfile(user.uid, function() {
// and once they safely exist, then we can delete the old ones
removeOldProfile();
removeOldLogin();
});
});
function copyProfile(newId, next) {
ref.child(oldUid).once('value', function(snap) {
if( snap.val() !== null ) {
ref.child(newId, snap.val(), function(err) {
logError(err);
if( !err ) { next(); }
});
}
});
}
function removeOldProfile() {
ref.child(oldId).remove(logError);
}
function createLogin(next) {
auth.createUser(newId, pass, function(err, user) {
logError(err);
if( !err ) { next(user); }
});
}
function removeOldLogin() {
auth.removeUser(oldId, pass, logError);
}
}
function logError(err) {
if( err ) { console.error(err); }
}

Meteor onCreateUser issue

I am using Meteor and people can connect to the site via Facebook. I'm using people username to identify them. But, some of them don't have a username. For example, a new user has null as a username. What I'm trying to do is, if the person has a username then OK we use their username. If not, I wanna use their Facebook id as their username. The problem is, my if condition is not working properly. If the person has a username, the if condition considers that the person doesn't. The weird thing is, if I do a console.log of the username before the if condition, it will show the username. But once in the if, it considers that the username is null. Here is the code :
Accounts.onCreateUser(function(options, user) {
var fb = user.services.facebook;
var token = user.services.facebook.accessToken;
if (options.profile) {
options.profile.fb_id = fb.id;
options.profile.gender = fb.gender;
options.profile.username = fb.username
console.log( 'username : ' + options.profile.username);
if ( !(options.profile.username === null || options.profile.username ==="null" || options.profile.username === undefined || options.profile.username === "undefined")) {
console.log('noooooooo');
options.profile.username = fb.id;
} else {
console.log('yessssssss');
options.profile.username = fb.username;
}
options.profile.email = fb.email;
options.profile.firstname = fb.first_name;
user.profile = options.profile;
}
sendWelcomeEmail(options.profile.name, options.profile.email);
return user;
});
With this code, if I log in with my Facebook that has a username. The condition will show 'noooooooo' but the console.log( 'username : ' + options.profile.username); will show my username. Why does it do that? :l
It's because creating is called before logging and logging is asynchronous .. so you cannot ensure that your if will be or will not true/false. Your putting information from fb service is redundant, because all these informations are already saved with user.
http://docs.meteor.com/#meteor_user
You should obtain information about user after he is loggedIn because in that moment you will be able to recognise what kind of identifier you can use username/id.
//Server side
Meteor.publish("userData", function () {
return Meteor.users.find({_id: this.userId});
// You can publish only facebook id..
/*return Meteor.users.find({_id: this.userId},
{
fields: {
'services.facebook.id': true
}
}
);*/
});
//Client side
Meteor.subscribe("userData");
// .. you can see more informations about logged user
console.log(Meteor.users.find({}).fetch());

Resources