2 lines
11 KiB
JavaScript
2 lines
11 KiB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).LandingSDK=t()}(this,function(){"use strict";if("function"!=typeof Object.keys||"function"!=typeof Date.now||"function"!=typeof Array.isArray){var e=function(){},t=function(){};t.landingPageView=e,t.landingPageClick=e;var n=function(){};if(n.init=e,n.track=t,"undefined"!=typeof globalThis)try{globalThis.LandingSDK=n}catch(e){}else"undefined"!=typeof window&&(window.LandingSDK=n);return n}function r(e){return r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},r(e)}var i,o="_s_token_",a="_d_token_",c="_d_sig_",u="_ping_flag_",d="data-app-id",l="app-id",f="event_id",p=null,v=null,g=null,s=null,y=function(e){var t=e.toString(16);return 1===t.length?"0"+t:t},h=function(){for(var e="",t=0;t<16;t++)e+=y(Math.floor(256*Math.random()));return e},w=function(){if(null!==s)return s;try{return window.localStorage.setItem(u,"1"),window.localStorage.removeItem(u),s=!0,!0}catch(e){return s=!1,!1}},_=function(){if("undefined"==typeof navigator)return"PC";var e=navigator.userAgent||"";return/Android/i.test(e)?"Android":/iPhone|iPad|iPod/i.test(e)?"iOS":"PC"},m=function(e){if(!e)return"";for(var t=e+"device_id_secret_key_v1",n=0,r=0;r<t.length;r++)n=(n<<5)-n+t.charCodeAt(r),n&=n;return Math.abs(n).toString(16)},k=function(){if("undefined"==typeof document)return"";for(var e=document.getElementsByTagName("script"),t=0;t<e.length;t++){var n=e[t],r=n.getAttribute(d)||n.getAttribute(l);if(r)return r}var i=document.currentScript||e[e.length-1];return i?i.getAttribute(d)||i.getAttribute(l):""},b=function(){try{var e="landing_device_fingerprint",t=w()&&window.localStorage.getItem(e)||"";if(t)return t;var n="undefined"!=typeof navigator?navigator:{},r="undefined"!=typeof screen?screen:{},i="undefined"!=typeof Intl&&Intl.DateTimeFormat&&Intl.DateTimeFormat().resolvedOptions().timeZone||"",o=n.language||"",a=Array.isArray(n.languages)?n.languages.join(","):"",c=r.colorDepth||"",u=r.pixelDepth||"",d=void 0!==r.width&&void 0!==r.height?"".concat(r.width,"x").concat(r.height):"",l=void 0!==r.availWidth&&void 0!==r.availHeight?"".concat(r.availWidth,"x").concat(r.availHeight):"",f="undefined"!=typeof window&&window.devicePixelRatio?window.devicePixelRatio:"",p=n.maxTouchPoints||0,v=n.hardwareConcurrency||"",g=n.deviceMemory||"",s=n.platform||"",y=[n.userAgent||"",s,n.vendor||"",o,a,i,(new Date).getTimezoneOffset(),d,l,c,u,f,p,v,g].join("||"),h="fp_".concat(function(){for(var e=2166136261,t=String((arguments.length>0&&void 0!==arguments[0]?arguments[0]:"")||""),n=0;n<t.length;n+=1)e^=t.charCodeAt(n),e=Math.imul(e,16777619);return(e>>>0).toString(16).padStart(8,"0")}(y));return function(t){if(t&&w())try{window.localStorage.setItem(e,t)}catch(e){}}(h),h}catch(e){return""}},S=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=function(){if("undefined"==typeof window)return"";if(w())try{var e=window.localStorage.getItem(a),t=window.localStorage.getItem(c);if(e&&!t){var n=h(),r=m(n);return window.localStorage.setItem(a,n),window.localStorage.setItem(c,r),n}if(e&&t){if(m(e)===t)return e;var i=h(),o=m(i);return window.localStorage.setItem(a,i),window.localStorage.setItem(c,o),i}var u=h(),d=m(u);return window.localStorage.setItem(a,u),window.localStorage.setItem(c,d),u}catch(e){}return g||(g=h()),g}(),n=function(){var e,t,n,r=(t=(e=new Date).getMonth()+1,n=e.getDate(),e.getFullYear()+"-"+(t<10?"0"+t:t)+"-"+(n<10?"0"+n:n));if("undefined"!=typeof window&&w())try{var i=window.localStorage.getItem(o);if(i)try{var a=JSON.parse(i);if(a.date===r&&a.sid)return a.sid}catch(e){}var c=h();return window.localStorage.setItem(o,JSON.stringify({sid:c,date:r})),c}catch(e){}return p&&v===r||(p=h(),v=r),p}(),r="",i="";return{app_id:e.appId||k(),uid:0,channel:e.channel||"",sid:n,client_ts:Math.floor(Date.now()/1e3),device:_(),device_id:t,user_agent:"undefined"==typeof navigator?"":navigator.userAgent||"",device_brand:r,device_model:i,fp_version:"1.0.0",device_fingerprint:b()}},j=function(){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1],t=arguments.length>2&&void 0!==arguments[2]&&arguments[2],n=(arguments.length>0&&void 0!==arguments[0]?arguments[0]:"")||(window.location.origin?window.location.origin:window.location.protocol+"//"+window.location.hostname+(window.location.port?":"+window.location.port:""));if(!n)return"";var r="/"===n.charAt(n.length-1)?n.slice(0,-1):n,i=e?"/dcserv":"",o=t?"/api/eventTracking/batchReport.json":"/api/eventTracking/report.json";return"".concat(r).concat(i).concat(o)},A=function(){for(var e={},t=0;t<arguments.length;t++){var n=arguments[t];if(n)for(var r=Object.keys(n),i=0;i<r.length;i++)e[r[i]]=n[r[i]]}return e},I=function(e){var t=(e=e||{}).endpoint?e.endpoint:j(e.domain,!0,e.batch)||"",n=function(e){return e=e||{},{sendPayload:function(t,n){if(n){var i,o;if(e.batch?(i=JSON.stringify(t),o="application/json"):(i=function(e){for(var t=Object.keys(e||{}),n=[],i=0;i<t.length;i++){var o=t[i],a=e[o];if(null!=a){var c="object"===r(a)?JSON.stringify(a):String(a);n.push(encodeURIComponent(o)+"="+encodeURIComponent(c))}}return n.join("&")}(t),o="application/x-www-form-urlencoded"),"undefined"!=typeof navigator&&"function"==typeof navigator.sendBeacon){var a=new Blob([i],{type:o});if(navigator.sendBeacon(n,a))return}try{var c=new XMLHttpRequest;c.open("POST",n,!0),c.timeout=1e4,c.setRequestHeader("Content-Type",o),c.send(i)}catch(e){}}}}}(A({endpoint:t},e));return{track:function(i,o){i=i||{};try{if(!i.event)throw new Error("event 字段必填");var a;if(e.batch){var c=i.event,u=A({},i);delete u.event,i={payload:u,event:c}}var d=function(e,t){var n=S(t),i=A({},n,e);return i.endpointOverride=void 0,i[f]||(i[f]=function(e){for(var t=e||{},n=Object.keys(t),i=[],o=0;o<n.length;o++)n[o]!==f&&i.push([n[o],t[n[o]]]);i.sort(function(e,t){return e[0].localeCompare(t[0])});for(var a=[],c=0;c<i.length;c++){var u=i[c][1];a.push(null==u?"":"object"===r(u)?JSON.stringify(u):String(u))}return function(e){e=e||"";for(var t=0,n=0;n<e.length;n+=1)t=(t<<5)-t+e.charCodeAt(n),t|=0;return"h"+Math.abs(t).toString(16)}(a.join("|"))}(i)),i}(A({},i),A({},e));if(!d.app_id)throw new Error("app_id 字段必填");if(void 0===e.channel||null===e.channel)throw new Error("channel 字段必填");a=e.batch?[d]:d;var l=o||(e.batch?j(e.domain,!0,!0):t);n.sendPayload(a,l)}catch(e){}}}},O={landingPageView:{landing_page_id:{type:"string",required:!0}},landingPageClick:{landing_page_id:{type:"string",required:!0},tab_key:{type:"string",required:!0},tab_name:{type:"string",required:!0},click_coordinates_x:{type:"int",required:!0},click_coordinates_y:{type:"int",required:!0},click_x_percent:{type:"int",required:!0},click_y_percent:{type:"int",required:!0},screen_width:{type:"int",required:!0},screen_height:{type:"int",required:!0},trace_id:{type:"string",required:!0}}},P={event_type:{click:!0,close:!0,show:!0},recommend_content_type:{video:!0,novel:!0,comic:!0},video_content_type:{video:!0,short_video:!0},video_behavior_key:{video_view:!0,video_play:!0,video_pause:!0,video_share:!0,video_complete:!0,video_forward:!0,video_rewind:!0}},T={ad_id:!0,creative_id:!0,recommend_id:!0,novel_tag_key:!0,comic_tag_key:!0,video_tag_key:!0},q=function(e){return String(null==e?"":e).trim()},x=function(e){return"number"==typeof e&&e%1==0},C=function(e){return x(e)?e:"string"==typeof e&&/^[+-]?\d+$/.test(e.trim())?parseInt(e.trim(),10):NaN},D=function(e,t,n,r){if(n)throw new TypeError("[sdk][event] "+e+"."+t+" "+r);return{keep:!1}},E=function(e,t,n,r){if(function(e){return/(^|_)(time|ts|timestamp)$/.test(e)}(t)){var i=C(n);return x(i)&&10===String(Math.abs(i)).length?{keep:!0,value:i}:D(e,t,r,"must be 10-digit int")}if(function(e){return/(^|_)(progress|percent)$/.test(e)}(t)){var o=C(n);return!x(o)||o<0||o>100?D(e,t,r,"must be int in [0,100]"):{keep:!0,value:o}}if(function(e){return"page_no"===e||"click_position"===e||/_position$/.test(e)}(t)){var a=C(n);return!x(a)||a<1?D(e,t,r,"must be int >= 1"):{keep:!0,value:a}}if("amount"===t||"coin_quantity"===t||"search_result_count"===t||"click_page_x"===t||"click_page_y"===t||"screen_width"===t||"screen_height"===t||"video_duration"===t||"play_duration"===t||"page_load_time"===t){var c=C(n);return!x(c)||c<=0?D(e,t,r,"must be int > 0"):{keep:!0,value:c}}if(T[t]){var u=q(n);if(!u)return D(e,t,r,"must not be empty");var d=u.split(",").map(function(e){return e.trim()}).filter(Boolean).join(",");return d?{keep:!0,value:d}:D(e,t,r,"must not be empty")}if(function(e){return/_key$/.test(e)}(t)){var l=q(n);return!l||l.length>64||!/^[A-Za-z0-9_]+$/.test(l)?D(e,t,r,"must match key rule"):{keep:!0,value:l}}if(function(e){return/_name$/.test(e)}(t)){var f=q(n);return f?{keep:!0,value:f.slice(0,128)}:D(e,t,r,"must not be empty")}if(function(e){return/(^id$|_id$)/.test(e)}(t)){var p=q(n);return p?{keep:!0,value:p.slice(0,128)}:D(e,t,r,"must not be empty")}if(function(e){return"comment_content"===e||"keyword"===e||/_title$/.test(e)||"novel_title"===e||"video_title"===e}(t)){var v=q(n),g="comment_content"===t||"keyword"===t?500:256;return v?{keep:!0,value:v.slice(0,g)}:D(e,t,r,"must not be empty")}if(Object.prototype.hasOwnProperty.call(P,t)){var s=q(n);return P[t][s]?{keep:!0,value:s}:D(e,t,r,"must be in enum whitelist")}return"string"==typeof n?{keep:!0,value:n.trim()}:{keep:!0,value:n}},M=function(e){return null===e?"null":Array.isArray(e)?"array":r(e)},$=function(e,t,n){t=t||{};var i=(n=n||{}).allowExtra,o=void 0!==i&&i,a=t&&"object"===r(t)&&!Array.isArray(t)?t:null;if(!a)throw new TypeError("[sdk][event] "+e+" payload must be an object");for(var c=O[e]||{},u=Object.keys(a),d={},l=0;l<u.length;l+=1){var f=u[l],p=c[f];if(!p){if(o){d[f]=a[f];continue}throw new TypeError("[sdk][event] "+e+"."+f+" is not allowed")}var v=E(e,f,a[f],!!p.required);if(v.keep){var g=M(v.value);if(p.type&&!("int"===p.type?"number"===g&&x(v.value):g===p.type))throw new TypeError("[sdk][event] "+e+"."+f+" expected "+p.type+", got "+g);d[f]=v.value}}for(var s=Object.keys(c),y=0;y<s.length;y+=1){var h=s[y];if(c[h].required&&!Object.prototype.hasOwnProperty.call(d,h))throw new TypeError("[sdk][event] "+e+"."+h+" is required")}return A({},d)},N={landingPageView:function(e,t){return A({event:"landing_page_view"},$("landingPageView",e||{},t||{}))},landingPageClick:function(e,t){return A({event:"landing_page_click"},$("landingPageClick",e||{},t||{}))}},R=null,L=function(e,t){return(R||(R=I()),R).track(e,t)};return L.track=function(e,t){return L(e,t)},i=L,Object.keys(N).forEach(function(e){var t=N[e];i.track[e]=function(e,n){try{return i.track(t(e),n)}catch(e){}}}),L.init=function(e){try{R=I(e||{})}catch(e){}},"undefined"!=typeof globalThis?globalThis.LandingSDK=L:"undefined"!=typeof window&&(window.LandingSDK=L),L});
|