var xcspm = (function () { var isDebug = true; function incjs(js, type) { type = type || 2; if (type == 1) { document.write(""); } else { var script = document.createElement('script'); script.src = js; document.body.appendChild(script); } } function debug(str, color) { if (isDebug == false) { return; } color = color || "green"; if (typeof str === "object") { console.log(str); } else { console.log("%c" + str, "color:" + color); } } function getsite() { var arr = { siteId: 0, channelId: 0, tableType: 0, tableId: 0, tag: 0, title: 0, url: 0, refUrl: 0 }; if (typeof visitConfig == "string") { //升成识别COOKIE var flagCookie = getcookie("ICMS_VISIT_FLAG_COOKIE"); if (flagCookie == undefined || flagCookie == "") { //写COOKIE var randCode = getRandCode(1000, 100000000); setcookie("ICMS_VISIT_FLAG_COOKIE", randCode); flagCookie = getcookie("ICMS_VISIT_FLAG_COOKIE"); } var arrConfig = visitConfig.split("||"); var funcUrl = decodeURIComponent(arrConfig[0]); var siteId = arrConfig[1]; var channelId = arrConfig[2]; var tableType = arrConfig[3]; var tableId = arrConfig[4]; var tag = arrConfig[5]; var title = encodeURI(document.title); var url = decodeURIComponent(window.location.href); var refUrl = decodeURIComponent(document.referrer); arr.siteId = siteId; arr.channelId = channelId; arr.tableType = tableType; arr.tableId = tableId; arr.tag = tag; arr.title = title; arr.url = url; arr.refUrl = refUrl; } return arr; } var spm=[]; var _opt = []; var _spm = { init: function (callback) { if (!window.spm) { // incjs("https://p.wts.xinwen.cn/dot-wts/spm.js"); debug("init spm"); } if (typeof upm === "undefined") { incjs("https://u.changsha.cn/jslib/?upm"); debug("init upm"); } if (typeof oc === "undefined") { incjs("https://u.changsha.cn/OcApi/"); debug("init oc"); } $(window).on("load", function () { // debug("spm loading!"); callback(); }); }, getcfg: function () { return _opt; }, config: function (opt, callback) { var normal_opt = { //常规参数 isDebug: false, userId: 0, sex: 1, profession: 1, age: 18, targetID: 0, organization: "zm5014", site_id: "zm5014-001" }; opt = $.extend(normal_opt, opt); console.log(opt); var spm_siteid = opt.site_id; var sitecfg = getsite(); opt.targetID = sitecfg.tableId;//设置siteid isDebug = opt.isDebug; _opt = opt; // debug(opt); var cfg = { siteId: spm_siteid,//网站标识 userId: opt.userId, // 此处填写已登录用户ID additionalInfo: { sex: opt.sex, // 此处填写已登录用户性别 profession: opt.profession, // 此处填写已登录用户职业 age: opt.age, // 此处填写已登录用户年龄 targetID: opt.targetID, // 此处填写稿件ID organization: opt.organization, // 此处填写媒体机构ID } }; spm.config(cfg); if (typeof oc === undefined) { debug(opt); debug(cfg); if (callback != undefined) { callback(); } } else { debug("has oc"); $(oc).one("onAuthApi", function (event, data, sign) { opt.userId = data.rect.user.userid; opt.age = data.rect.userinfo.age; opt.profession = data.rect.userinfo.occupational; opt.sex = data.rect.userinfo.gendar; debug(opt); var cfg = { siteId: spm_siteid,//网站标识 userId: opt.userId, // 此处填写已登录用户ID additionalInfo: { sex: opt.sex, // 此处填写已登录用户性别 profession: opt.profession, // 此处填写已登录用户职业 age: opt.age, // 此处填写已登录用户年龄 targetID: opt.targetID, // 此处填写稿件ID organization: opt.organization, // 此处填写媒体机构ID } }; spm.config(cfg); debug(cfg); if (callback != undefined) { callback(); } }); oc.checklogin(); } // console.log(spm); // window.onbeforeunload = function () { // // 上报离开事件 // this.push('leave'); // }; window.onbeforeunload = function () { // 立即上报事件,如果要检验上报效果建议使用这种方式 spm.push({category: 'event', action: 'leave'}); }; }, //事件类型 push: function (action) { spm.push({category: 'event', action: action}); debug("事件上报"); }, //点赞上报 praise: function (callback) { var data = { category: 'event', action: 'praise', targetURL: window.location.href, }; spm.push(data); if (typeof callback === "function") { callback(data); } debug("点赞上报") }, //评论上报 comment: function (comment, callback) { var data = { category: 'event', action: 'comment', comment: comment, targetURL: window.location.href, }; spm.push(data); if (typeof callback === "function") { callback(data); } debug("评论上报") }, // 转发上报 forword: function (url, callback) { // 站点的转发功能 var data = {category: 'event', action: 'forward', "targetURL": url}; spm.push(data); if (typeof callback === "function") { callback(data); } debug("转发上报"); } }; return _spm; }(window.jQuery || $)); //调用示例 if (typeof xcspm === "undefined") { console.log("no spm"); } else { xcspm.init(function () { // xcspm.config({ // isDebug: true, // }, // function () { // console.log("spm callback"); // } // ); // console.log(xcspm.getcfg()); // //评论上报 // xcspm.comment("评论上报", function (data) { // console.log(data); // }); // //转发上报 // xcspm.forword("", function (data) { // console.log(data); // }); // //点赞上报 // xcspm.praise(function (data) { // console.log(data); // }); }); }