概要
Yahooとの連携でYahooのユーザIDとCxenseユーザIDがリンク可能となり、オーディエンスセグメント、レポート作成、ターゲティングに使用することができます。
セットアップ手順
CxenseユーザーIDとYahooユーザーIDを取得し、Cxense APIで2つのユーザーIDをマッピングさせる必要があります。
-
アクセス中のYahooユーザID(Yahooアカウントでログインしていることが前提です)を取得します。 ページにYahooサインインを追加する方法は https://developer.yahoo.com/oauth2/guide/ を参照してください。
-
通常通りCxense Insightのタグを全ページに挿入してください。これにより、Webサーバーが受信するすべてのHTTPリクエストにCxenseユーザーIDがCookieとして追加されます。
-
サーバー上のYahoo OathコールバックURLハンドラで、CookieからCxense IDを読み取るコードと、認証結果からYahoo IDを読み取るコードを追加します(例については下記を参照)。
ユーザーをリンクするには、Cxense API /profile/user/external/link/update を利用します。
コードサンプルの一部
// In the Yahoo Oath callback URL handler, map the user IDs:
app.get('/auth/yahoo/callback',
passport.authenticate('yahoo', { failureRedirect: '/login.html' }),
function(req, res) {
// Successful authentication, link the user IDs and then redirect home.
var cxenseId = req.cookies['cX_P'];
var yahooId = req.user.id;
console.log('Mapping user cxense ID: ' + cxenseId + ' and Yahoo ID: ' + yahooId);
// The actual linking of IDs:
cxApiRequest('/profile/user/external/link/update', { type: prefix, id: yahooId, cxid: cxenseId },
function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log('Link update success!');
} else {
console.log('Map error: ' + (error || response.statusCode))
}
});
res.redirect('/');
});
Yahooのログインを追加しYahoo!ユーザーIDをCxenseのユーザーIDにリンクするサーバーサイドのコードサンプル
JavaScript
var crypto = require('crypto'),
http = require('http'),
express = require('express'),
expressSession = require('express-session'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
methodOverride = require('method-override'),
passport = require('passport'),
YahooStrategy = require('passport-yahoo-oauth2').OAuth2Strategy,
request = require('request');
// The Cxense API request setup
var username = 'your@email.com';// TODO: Add your Cxense username here
var apiKey = 'Cxense API KEY'; // TODO: Add your Cxense API key here
var prefix = 'xyz'; // TODO: Add your Cxense customer prefix here
function cxApiRequest(apiName, body, callback) {
var date = new Date().toISOString(), hmac = crypto.createHmac('sha256', apiKey).update(date).digest('hex');
console.log('Sending API request: ' + apiName + ', body: ' + JSON.stringify(body));
request.post({
url: 'https://api.cxense.com' + apiName,
headers: {'X-cXense-Authentication': 'username=' + username + ' date=' + date + ' hmac-sha256-hex=' + hmac},
body: body,
json: true
}, callback);
}
// The Yahoo sign-in setup (using Passport and Express)
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the complete Google profile is
// serialized and deserialized.
passport.serializeUser(function(user, done) {
done(null, user);
});
passport.deserializeUser(function(obj, done) {
done(null, obj);
});
passport.use(new YahooStrategy({
clientKey: 'Consumer Key', // TODO: Add your Yahoo consumer key here
clientSecret: 'Consumer Secret', // TODO: Add your Yahoo consumer secret here
callbackURL: 'https://cxensebot.cxense.com/auth/yahoo/callback'
},
function(token, tokenSecret, auth, profile, cb) {
cb(null, profile);
}
));
// Configure Express
var app = express();
var server = http.createServer(app);
app.set('port', 10520);
app.use(bodyParser.json());
app.use(cookieParser());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride());
app.use(expressSession({ secret: 'funny figure', resave: false, saveUninitialized: false }));
// Initialize Passport! Also use passport.session() middleware, to support persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.get('/auth/yahoo',
passport.authenticate('yahoo'));
app.get('/auth/yahoo/callback',
passport.authenticate('yahoo', { failureRedirect: '/login.html' }),
function(req, res) {
// Successful authentication, link the user IDs and then redirect home.
var cxenseId = req.cookies['cX_P'];
var yahooId = req.user.id;
console.log('Mapping user cxense ID: ' + cxenseId + ' and yahoo ID: ' + yahooId);
// The actual linking of IDs:
cxApiRequest('/profile/user/external/link/update', { type: prefix, id: yahooId, cxid: cxenseId },
function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log('Link update success!');
} else {
console.log('Map error: ' + (error || response.statusCode))
}
});
res.redirect('/');
});
// Start server
server.listen(app.get('port'));
console.log('Listening on port ' + app.get('port'));