@vizertech/nextjs-capi
Version:
NextJS 13 Wrapper for fb pixel and capi for graph v17.0 API
46 lines (45 loc) • 1.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClientFbq = exports.getClientFbc = exports.getClientUserAgent = exports.getClientIpAddress = void 0;
const universal_cookie_1 = __importDefault(require("universal-cookie"));
function getClientIpAddress(req) {
var _a;
const ip = req.headers['x-real-ip'] || req.connection.remoteAddress;
if (ip) {
return ip;
}
const xFF = (_a = req.headers['x-forwarded-for']) !== null && _a !== void 0 ? _a : '';
return xFF.split(',')[0];
}
exports.getClientIpAddress = getClientIpAddress;
function getClientUserAgent(req) {
var _a;
return (_a = req.headers['user-agent']) !== null && _a !== void 0 ? _a : '';
}
exports.getClientUserAgent = getClientUserAgent;
function getClientFbq(req) {
const cookies = new universal_cookie_1.default(req.headers.cookie);
if (!cookies.get('_fbp')) {
return '';
}
return cookies.get('_fbp');
}
exports.getClientFbq = getClientFbq;
function getClientFbc(req) {
var _a;
if (req.headers.referer) {
const url = new URL(req.headers.referer);
if (url.searchParams.has('fbclid')) {
return (_a = url.searchParams.get('fbclid')) !== null && _a !== void 0 ? _a : '';
}
}
const cookies = new universal_cookie_1.default(req.headers.cookie);
if (cookies.get('_fbc')) {
return cookies.get('_fbc');
}
return '';
}
exports.getClientFbc = getClientFbc;