pixel_h5.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. var pixels = [];
  2. Date.prototype.Format = function (fmt) {
  3. var o = {
  4. 'M+': this.getMonth() + 1, //月份
  5. 'd+': this.getDate(), //日
  6. 'H+': this.getHours(), //小时
  7. 'm+': this.getMinutes(), //分
  8. 's+': this.getSeconds(), //秒
  9. 'q+': Math.floor((this.getMonth() + 3) / 3), //季度
  10. S: this.getMilliseconds() //毫秒
  11. };
  12. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  13. for (var k in o)
  14. if (new RegExp('(' + k + ')').test(fmt))
  15. fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
  16. return fmt;
  17. };
  18. try {
  19. var paste = new Date().Format('yyyyMMddHHmmss') + Math.ceil(Math.random() * 10000000);
  20. } catch (e) {
  21. console.log('create paste error');
  22. }
  23. // 初始化状态表
  24. var fbcalllist = {
  25. fbinited: null,
  26. gginited: null,
  27. kkinited: null,
  28. ttinited: null,
  29. baseLeftInitCount: 4,
  30. isInited: function () {
  31. return fbcalllist.baseLeftInitCount == 0;
  32. },
  33. initEndCallback: [],
  34. addInitEndCallback: function (callback) {
  35. fbcalllist.initEndCallback.push(callback);
  36. },
  37. noticeInitEnd: function (isTimeout = false) {
  38. if (isTimeout == true) {
  39. console.log('timeout');
  40. }
  41. if (fbcalllist.baseLeftInitCount == 0 || isTimeout == true) {
  42. for (var i = 0; i < fbcalllist.initEndCallback.length; i++) {
  43. fbcalllist.initEndCallback[i]();
  44. }
  45. // 去除监听回调
  46. fbcalllist.initEndCallback = [];
  47. }
  48. },
  49. // 模块回调
  50. fbInitEnd: function () {
  51. if (!fbcalllist.fbinited) {
  52. fbcalllist.baseLeftInitCount = fbcalllist.baseLeftInitCount - 1;
  53. fbcalllist.fbinited = true;
  54. fbcalllist.noticeInitEnd();
  55. }
  56. },
  57. ggInitEnd: function () {
  58. if (!fbcalllist.gginited) {
  59. fbcalllist.baseLeftInitCount = fbcalllist.baseLeftInitCount - 1;
  60. fbcalllist.gginited = true;
  61. fbcalllist.noticeInitEnd();
  62. }
  63. },
  64. kkInitEnd: function () {
  65. if (!fbcalllist.kkinited) {
  66. fbcalllist.baseLeftInitCount = fbcalllist.baseLeftInitCount - 1;
  67. fbcalllist.kkinited = true;
  68. fbcalllist.noticeInitEnd();
  69. }
  70. },
  71. ttInitEnd: function () {
  72. if (fbcalllist.ttinited != true) {
  73. fbcalllist.baseLeftInitCount = fbcalllist.baseLeftInitCount - 1;
  74. fbcalllist.ttinited = true;
  75. fbcalllist.noticeInitEnd();
  76. }
  77. }
  78. };
  79. fbcalllist.kkInitEnd();
  80. fbcalllist.ttInitEnd();
  81. //记录 pixcel 值
  82. function load_pixel(pixel) { }
  83. function getCookie(name) {
  84. const value = `; ${document.cookie}`;
  85. const parts = value.split(`; ${name}=`);
  86. if (parts.length === 2) return parts.pop().split(';').shift();
  87. }
  88. var fbevapi = '';
  89. var evapi = '';
  90. var dualstackApi = '';
  91. var isdebug = false;
  92. if (
  93. window.location.href.indexOf('localhost') >= 0 ||
  94. window.location.href.indexOf('192.168.1.') >= 0 ||
  95. window.location.href.indexOf('192.168.11.') >= 0 ||
  96. window.location.href.indexOf('10.10.100.') >= 0 ||
  97. window.location.href.indexOf('usa3.cjwend.in') >= 0
  98. ) {
  99. isdebug = true;
  100. } else {
  101. isdebug = false;
  102. }
  103. evapi = window.themeConfig.api;
  104. fbevapi = evapi;
  105. dualstackApi = window.themeConfig.api;
  106. evapi = evapi + '/api/weblog';
  107. //qilinid 预加载(下载的时候有用)
  108. var preloadqilinid = localStorage.getItem('qilinid');
  109. if (preloadqilinid != null && preloadqilinid != '') {
  110. window.qilinid = preloadqilinid;
  111. }
  112. // 如果有链接变化, 则保存到 localstorage 里面
  113. function checkAdUrlChange() {
  114. // 如果当前的链接里面有 pixel , gcode 参数, 则保存到 localstorage 里面
  115. var url = window.location.href;
  116. var urlParams = new URLSearchParams(location.search);
  117. var pixel = urlParams.get('pixel');
  118. var gcode = urlParams.get('gcode');
  119. if ((pixel != null && pixel != '') || (gcode != null && gcode != '')) {
  120. localStorage.setItem('lastFbUrl', url);
  121. }
  122. }
  123. checkAdUrlChange();
  124. // 封装的目的 因为前端框架的路由变化的原因, 会导致页面的 url 变化, 但是页面的 fbclid 参数不变
  125. function getURLSearchParams() {
  126. // 如果 local storage 里面有, 则生成返回
  127. var lastFbUrl = localStorage.getItem('lastFbUrl');
  128. if (lastFbUrl != null && (lastFbUrl.indexOf('fbclid') >= 0 || lastFbUrl.indexOf('gcode') >= 0)) {
  129. // 先使用 ? 分割
  130. var url = lastFbUrl.split('?');
  131. if (url.length >= 2) {
  132. return new URLSearchParams(url[1]);
  133. } else {
  134. return new URLSearchParams(lastFbUrl);
  135. }
  136. }
  137. // 如果没有, 则从当前 url 里面获取
  138. return new URLSearchParams(location.search);
  139. }
  140. var hasGGSendPageView = false;
  141. var hasSendPageView = false;
  142. function checkFbcAndFbp(conditionname) {
  143. // console.log('check ...', conditionname);
  144. if (hasSendPageView) {
  145. console.log('hasSendPageView');
  146. return;
  147. }
  148. var fbp = getCookie('_fbp');
  149. var fbc = getCookie('_fbc');
  150. if (fbp == undefined) {
  151. // console.log("fbp is null", fbc, fbp);
  152. setTimeout(function () {
  153. checkFbcAndFbp('timer check');
  154. }, 10);
  155. return;
  156. }
  157. fbcalllist.fbInitEnd(); // 取到参数就是加载结束
  158. // 如果 url 里面没有 fbclid, 则不做提取拦截
  159. var url = window.location.href;
  160. if (url.indexOf('fbclid') >= 0) {
  161. if (fbc == undefined) {
  162. // console.log("fbc is null", fbc, fbp);
  163. return;
  164. }
  165. } else {
  166. // 检查 localstorage 里面是否有上次的 fbclid
  167. var lastFbUrl = localStorage.getItem('lastFbUrl');
  168. if (lastFbUrl != null && lastFbUrl.indexOf('fbclid') >= 0) {
  169. return;
  170. }
  171. }
  172. submitFBEvent('PageView');
  173. hasSendPageView = true;
  174. }
  175. //多个像素初始化
  176. function initPixels(pixels_array, useOfficialOnly) {
  177. console.log('old call');
  178. }
  179. function init_facebook_pixels(pixels) {
  180. console.log('old call 2');
  181. }
  182. function loadpixel() {
  183. const urlParams = getURLSearchParams();
  184. var pixel = urlParams.get('pixel');
  185. if (pixel == undefined || pixel == null || pixel == '') {
  186. fbcalllist.fbInitEnd();
  187. return;
  188. }
  189. !(function (f, b, e, v, n, t, s) {
  190. if (f.fbq) return;
  191. n = f.fbq = function () {
  192. n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments);
  193. };
  194. if (!f._fbq) f._fbq = n;
  195. n.push = n;
  196. n.loaded = !0;
  197. n.version = '2.0';
  198. n.queue = [];
  199. t = b.createElement(e);
  200. t.addEventListener('load', function () {
  201. fbq('init', pixel);
  202. var u = navigator.userAgent;
  203. console.log('userAgent = ', u);
  204. var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  205. var isMac = u.indexOf('Macintosh') >= 0;
  206. if (isiOS || isMac) {
  207. console.log('is ios or mac');
  208. //fbq('init', '767609381614056');
  209. // fbq('init', '307708915641269');// 备用
  210. //fbq('init', '1214268193069234');
  211. // fbq('init', '430357023093521');// 备用1
  212. // fbq('init', '3348497502121560');// 备用2
  213. } else {
  214. //fbq('init', '410150778670828');
  215. // fbq('init', '950445440166110'); // 备用1
  216. // fbq('init', '1710862789652432'); // 备用2
  217. }
  218. fbq('track', 'PageView');
  219. checkFbcAndFbp('after laod node');
  220. });
  221. t.async = !0;
  222. t.src = v;
  223. s = b.getElementsByTagName(e)[0];
  224. s.parentNode.insertBefore(t, s);
  225. })(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js');
  226. }
  227. try {
  228. loadpixel();
  229. } catch (e) {
  230. console.log('load pixel error:', e);
  231. }
  232. // 复制 qilinid 到剪切板
  233. function copyQilinid() {
  234. if (!(window.qilinid == undefined || window.qilinid == null || window.qilinid == '')) {
  235. console.log('qilinid is copyed');
  236. if (copyText(window.qilinid)) {
  237. window.qilinid = ''; //就拷贝一次就行
  238. }
  239. } else {
  240. // alert('qilin id is null');
  241. }
  242. }
  243. function copyText(text) {
  244. var input = document.createElement('textarea');
  245. document.body.appendChild(input);
  246. // 将文本赋值给输入框
  247. input.value = text;
  248. // 聚焦并选中
  249. input.focus();
  250. input.select();
  251. var result = false;
  252. try {
  253. if (document.queryCommandEnabled('copy')) {
  254. // 执行 copy 命令
  255. var success = document.execCommand('copy');
  256. // 移除输入框节点
  257. console.log('Copy Ok');
  258. result = success;
  259. // alert('success copy');
  260. } else {
  261. console.log('queryCommandEnabled is false');
  262. result = false;
  263. // alert('failed copy');
  264. }
  265. } catch (e) {
  266. // alert('failed copy catch exception');
  267. console.log(e);
  268. }
  269. input.remove();
  270. return result;
  271. }
  272. function requestEvApi(requsetUrl, callback) {
  273. var url = requsetUrl;
  274. var xhr = new XMLHttpRequest();
  275. xhr.open('GET', url, true);
  276. xhr.send();
  277. xhr.onload = function (data) {
  278. if (callback) {
  279. callback(this.responseText);
  280. }
  281. if (this.responseText.indexOf('Error No Access Token Find') >= 0) {
  282. console.error(this.responseText);
  283. // alert("Please config access token : " + this.responseText);
  284. } else {
  285. console.log(this.responseText);
  286. // console.log('request success');
  287. }
  288. };
  289. }
  290. //提交 FB 事件
  291. function submitFBEvent(eventName) {
  292. // 获取 cookie 里面的 fbp 参数
  293. var fbp = getCookie('_fbp');
  294. if (fbp == null) {
  295. fbp = '';
  296. console.log('fbp is null');
  297. } else {
  298. console.log('fbp is ' + fbp);
  299. }
  300. var fbc = getCookie('_fbc');
  301. // 从 url 里面获取 material 参数
  302. const urlParams = getURLSearchParams();
  303. var pixel = urlParams.get('pixel'); // fb 像素
  304. var material = urlParams.get('campaign'); // 取广告系列名
  305. var adgroup = urlParams.get('adgroup'); // 取广告系组名
  306. if (material == null) {
  307. material = '';
  308. }
  309. if (adgroup == null) {
  310. adgroup = '';
  311. }
  312. var apkch = urlParams.get('apkch') || '';
  313. if (apkch == '') {
  314. apkch = window.apkch || '';
  315. }
  316. if (window.apkchforce != undefined && window.apkchforce != null && window.apkchforce != '') {
  317. apkch = window.apkchforce || '';
  318. }
  319. // 查找 url 里面正则提取 ch_ 开头的协议
  320. if (apkch == '') {
  321. var lourl = window.location.href;
  322. var reg = /ch_([^/]*)/g;
  323. var match = reg.exec(lourl);
  324. if (match != null && match.length >= 2) {
  325. apkch = match[1];
  326. }
  327. }
  328. var paste = '';
  329. // 上报一次到服务器. 只要 fbc 不变化, 那就只上传一次. 因为 h5 里面访问会很高频。
  330. var reportfbc = localStorage.getItem('reportfbc');
  331. var oldqilinid = localStorage.getItem('qilinid');
  332. if (reportfbc == null || reportfbc != fbc || oldqilinid == null || oldqilinid == '') {
  333. var url =
  334. dualstackApi +
  335. '/api/sendfbevent?pixel=' +
  336. pixel +
  337. '&apkch=' +
  338. apkch +
  339. '_APK' +
  340. '&event=' +
  341. eventName +
  342. '&fbp=' +
  343. fbp +
  344. '&fbc=' +
  345. fbc +
  346. '&paste=' +
  347. paste +
  348. '&material=' +
  349. material +
  350. '&adgroup=' +
  351. adgroup +
  352. '&dualstack=1';
  353. requestEvApi(url, function (data) {
  354. //从 data 解析 qilinid
  355. var qilinid = '';
  356. if (data != undefined && data != null) {
  357. // 正则匹配 qilinid(?) 括号里面的内容
  358. var reg = /qilinid\((.*?)\)/g;
  359. var match = reg.exec(data);
  360. if (match != null && match.length >= 2) {
  361. qilinid = match[1];
  362. }
  363. window.qilinid = qilinid;
  364. // 保存到 localstorage 里面
  365. localStorage.setItem('qilinid', qilinid);
  366. localStorage.setItem('reportfbc', fbc);
  367. }
  368. });
  369. } else {
  370. window.qilinid = oldqilinid;
  371. }
  372. //fb 的事件提交
  373. fbq('track', eventName); //'SubmitApplication'
  374. }
  375. // 页面统计事件
  376. function pageEventSubmit(event_name, during = 0, mark = '') {
  377. // 请求 evapi 接口
  378. // alert(url);
  379. // XMLHttpRequest post application/json
  380. var xhr = new XMLHttpRequest();
  381. var urlParams = getURLSearchParams();
  382. var click_id = urlParams.get('fbclid') || '';
  383. var apkch = urlParams.get('apkch') || '';
  384. if (apkch == '') {
  385. apkch = window.apkch || '';
  386. }
  387. if (window.apkchforce != undefined && window.apkchforce != null && window.apkchforce != '') {
  388. apkch = window.apkchforce || '';
  389. }
  390. // 查找 url 里面正则提取 ch_ 开头的协议
  391. if (apkch == '') {
  392. var lourl = window.location.href;
  393. var reg = /ch_([^/]*)/g;
  394. var match = reg.exec(lourl);
  395. if (match != null && match.length >= 2) {
  396. apkch = match[1];
  397. }
  398. }
  399. xhr.open('POST', evapi, true);
  400. // xhr.setRequestHeader("Content-Type","application/json");
  401. var data = JSON.stringify({
  402. click_id: click_id,
  403. event_name: event_name,
  404. apkch: apkch,
  405. during: during,
  406. mark: mark
  407. });
  408. xhr.send(data);
  409. xhr.onload = function (data) {
  410. console.log(this.responseText);
  411. // console.log('request success');
  412. };
  413. }
  414. var retryCountJs = 0;
  415. var lastclickDownloadTime = 0;
  416. // 页面下载
  417. function pageDownload(download_url) {
  418. // 取到 fbp 之后就去下载
  419. var retryDownTime = 0;
  420. function autoDownload() {
  421. retryDownTime = retryDownTime + 1;
  422. if (retryDownTime > 2 * 10) {
  423. //超过 2 秒, 就直接请求
  424. pageEventSubmit(down_type_name, 0, download_url);
  425. // window.location.href = download_url
  426. todoownload(download_url);
  427. return;
  428. }
  429. if (hasSendPageView == false && hasGGSendPageView == false) {
  430. setTimeout(() => {
  431. autoDownload();
  432. }, 100); // 过100毫秒再检查
  433. } else {
  434. todoownload(download_url);
  435. }
  436. }
  437. autoDownload();
  438. return;
  439. }
  440. // 使用 iframe 来 做 url apk 下载
  441. function todoownload(download_url) {
  442. if (download_url == undefined || download_url == null || download_url == '') {
  443. return;
  444. }
  445. downloadIframe = document.createElement('iframe');
  446. downloadIframe.style.display = 'none';
  447. document.body.appendChild(downloadIframe);
  448. downloadIframe.src = download_url;
  449. }
  450. // -- google
  451. function checkGGClientEvent(conditionname) {
  452. if (hasGGSendPageView) {
  453. console.log('hasGGSendPageView');
  454. return;
  455. }
  456. if (window.gg_client_id == undefined) {
  457. setTimeout(function () {
  458. checkGGClientEvent('timer check');
  459. }, 10);
  460. return;
  461. }
  462. submitGGEvent('page_view');
  463. hasGGSendPageView = true;
  464. }
  465. // 谷歌加载
  466. (function () {
  467. const urlParams = getURLSearchParams();
  468. var gcode = urlParams.get('gcode');
  469. if (gcode == undefined || gcode == null || gcode == '') {
  470. fbcalllist.ggInitEnd();
  471. return;
  472. }
  473. var script = document.createElement('script');
  474. script.async = true;
  475. script.src = 'https://www.googletagmanager.com/gtag/js?id=' + gcode;
  476. document.head.appendChild(script);
  477. window.dataLayer = window.dataLayer || [];
  478. function gtag() {
  479. dataLayer.push(arguments);
  480. }
  481. script.onload = function () {
  482. gtag('js', new Date());
  483. gtag('config', gcode);
  484. gtag('get', gcode, 'client_id', clientID => {
  485. console.log('this client id is: ' + clientID);
  486. window.gg_client_id = clientID;
  487. fbcalllist.ggInitEnd();
  488. });
  489. checkGGClientEvent('script.onload');
  490. };
  491. })();
  492. //提交 GG 事件
  493. function submitGGEvent(eventName) {
  494. // 从 url 里面获取 material 参数
  495. const urlParams = new URLSearchParams(location.search);
  496. var gcode = urlParams.get('gcode'); // 谷歌代码
  497. if (gcode == null || gcode == '') {
  498. return false;
  499. }
  500. var gclid = urlParams.get('gclid') || ''; // 点击id
  501. var material = urlParams.get('campaign'); // 取广告系列名
  502. var adgroup = urlParams.get('adgroup'); // 取广告系组名
  503. if (material == null) {
  504. material = '';
  505. }
  506. if (adgroup == null) {
  507. adgroup = '';
  508. }
  509. var apkch = urlParams.get('apkch') || '';
  510. if (apkch == '') {
  511. apkch = window.apkch || '';
  512. }
  513. if (window.apkchforce != undefined && window.apkchforce != null && window.apkchforce != '') {
  514. apkch = window.apkchforce || '';
  515. }
  516. // 查找 url 里面正则提取 ch_ 开头的协议
  517. if (apkch == '') {
  518. var lourl = window.location.href;
  519. var reg = /ch_([^/]*)/g;
  520. var match = reg.exec(lourl);
  521. if (match != null && match.length >= 2) {
  522. apkch = match[1];
  523. }
  524. }
  525. var paste = '';
  526. var fbp = 'gg.' + (window.gg_client_id || '');
  527. var fbc = 'gg.' + (gclid || '');
  528. var url =
  529. dualstackApi +
  530. '/api/sendggevent?gcode=' +
  531. gcode +
  532. '&apkch=' +
  533. apkch +
  534. '_APK' +
  535. '&event=' +
  536. eventName +
  537. '&fbp=' +
  538. fbp +
  539. '&fbc=' +
  540. fbc +
  541. '&paste=' +
  542. paste +
  543. '&material=' +
  544. material +
  545. '&adgroup=' +
  546. adgroup;
  547. var reportgclid = localStorage.getItem('reportgclid');
  548. var oldqilinid = localStorage.getItem('qilinid');
  549. if (reportgclid == null || reportgclid != gclid || oldqilinid == null || oldqilinid == '') {
  550. requestEvApi(url, function (data) {
  551. //从 data 解析 qilinid
  552. var qilinid = '';
  553. if (data != undefined && data != null) {
  554. // 正则匹配 qilinid(?) 括号里面的内容
  555. var reg = /qilinid\((.*?)\)/g;
  556. var match = reg.exec(data);
  557. if (match != null && match.length >= 2) {
  558. qilinid = match[1];
  559. }
  560. window.qilinid = qilinid;
  561. localStorage.setItem('qilinid', qilinid);
  562. localStorage.setItem('reportgclid', gclid);
  563. }
  564. if (window.callLoadGtag != undefined) {
  565. try {
  566. window.callLoadGtag();
  567. } catch (e) {
  568. console.log(e);
  569. }
  570. window.callLoadGtag = undefined;
  571. }
  572. });
  573. } else {
  574. window.qilinid = oldqilinid;
  575. }
  576. return true;
  577. }
  578. function sureCheck() {
  579. if (fbcalllist.isInited()) {
  580. // 通知回调
  581. fbcalllist.noticeInitEnd();
  582. } else if (new Date().getTime() - fbcalllist.startCheckTime > fbcalllist.timeOutSeconds * 1000) {
  583. // 已超时, 直接通知掉
  584. fbcalllist.noticeInitEnd(true);
  585. } else {
  586. // 100 毫秒后再次检查
  587. setTimeout(function () {
  588. sureCheck();
  589. }, 100);
  590. }
  591. }
  592. // * 在网页内启动的时候, 调用这个方法来获得 fbp , fbc , gclid , material , adgroup, apkch
  593. function getFbcAndFbp(bccallback, timeOutSeconds = 5) {
  594. fbcalllist.startCheckTime = new Date().getTime();
  595. fbcalllist.timeOutSeconds = timeOutSeconds;
  596. // 加入监听
  597. fbcalllist.addInitEndCallback(function () {
  598. var fbinfo = {};
  599. try {
  600. // 从 url 里面获取 material 参数
  601. const urlParams = getURLSearchParams(); //new URLSearchParams(location.search);
  602. var fbinfo = {
  603. fbp: undefined,
  604. fbc: undefined,
  605. ready: false,
  606. gclid: undefined,
  607. pixel: undefined,
  608. gcode: undefined,
  609. material: undefined,
  610. adgroup: undefined,
  611. apkch: undefined,
  612. paste: undefined,
  613. creative: urlParams.get('creative')
  614. };
  615. // 遍历打印 urlParams
  616. // for (var pair of urlParams.entries()) {
  617. // console.log(pair[0]+ ', ' + pair[1]);
  618. // }
  619. fbinfo.gcode = urlParams.get('gcode'); // 谷歌代码
  620. fbinfo.gclid = urlParams.get('gclid') || ''; // 点击id
  621. fbinfo.fbclid = urlParams.get('fbclid') || ''; // 点击id
  622. fbinfo.material = urlParams.get('campaign'); // 取广告系列名
  623. fbinfo.adgroup = urlParams.get('adgroup'); // 取广告系组名
  624. if (fbinfo.material == null) {
  625. fbinfo.material = '';
  626. }
  627. if (fbinfo.adgroup == null) {
  628. fbinfo.adgroup = '';
  629. }
  630. var apkch = urlParams.get('apkch') || '';
  631. if (apkch == '') {
  632. apkch = window.apkch || '';
  633. }
  634. if (window.apkchforce != undefined && window.apkchforce != null && window.apkchforce != '') {
  635. apkch = window.apkchforce || '';
  636. }
  637. // 查找 url 里面正则提取 ch_ 开头的协议
  638. if (apkch == '') {
  639. var lourl = window.location.href;
  640. var reg = /ch_([^/]*)/g;
  641. var match = reg.exec(lourl);
  642. if (match != null && match.length >= 2) {
  643. apkch = match[1];
  644. }
  645. }
  646. fbinfo.apkch = apkch;
  647. fbinfo.paste = '';
  648. // fb 点击进来的
  649. if (fbinfo.fbclid != undefined && fbinfo.fbclid != null && fbinfo.fbclid != '') {
  650. fbinfo.fbp = getCookie('_fbp');
  651. fbinfo.fbc = getCookie('_fbc');
  652. fbinfo.pixel = urlParams.get('pixel'); // fb 像素
  653. }
  654. //
  655. if (fbinfo.gcode != undefined && fbinfo.gcode != null && fbinfo.gcode != '') {
  656. fbinfo.fbp = 'gg.' + (window.gg_client_id || '');
  657. fbinfo.fbc = 'gg.' + (fbinfo.gclid || '');
  658. fbinfo.pixel = fbinfo.gcode;
  659. }
  660. fbinfo.ready = false;
  661. if (
  662. fbinfo.fbp != undefined &&
  663. fbinfo.fbp != null &&
  664. fbinfo.fbp != '' &&
  665. fbinfo.fbc != undefined &&
  666. fbinfo.fbc != null &&
  667. fbinfo.fbc != ''
  668. ) {
  669. fbinfo.ready = true;
  670. }
  671. // campaign , campaign_id 的提取
  672. if (fbinfo.material != undefined && fbinfo.material != null && fbinfo.material != '') {
  673. // fb
  674. if (fbinfo.fbp != undefined && fbinfo.fbp != null && fbinfo.fbp != '' && fbinfo.fbp.indexOf('fb.') >= 0) {
  675. // "{{campaign.name}} ({{campaign.id}})" 解析出 campaign.name 和 campaign.id
  676. var reg = /(.*)\((.*)\)/g;
  677. var match = reg.exec(fbinfo.material);
  678. if (match != null && match.length >= 3) {
  679. fbinfo.campaign = match[1];
  680. fbinfo.campaign_id = match[2];
  681. }
  682. }
  683. // gg
  684. if (fbinfo.fbp != undefined && fbinfo.fbp != null && fbinfo.fbp != '' && fbinfo.fbp.indexOf('gg.') >= 0) {
  685. fbinfo.campaign = fbinfo.material;
  686. fbinfo.campaign_id = fbinfo.material;
  687. }
  688. }
  689. // ad , adset , adset_id
  690. if (fbinfo.adgroup != undefined && fbinfo.adgroup != null && fbinfo.adgroup != '') {
  691. // fb
  692. if (fbinfo.fbp != undefined && fbinfo.fbp != null && fbinfo.fbp != '' && fbinfo.fbp.indexOf('fb.') >= 0) {
  693. // "{{adset.name}} ({{adset.id}})" 解析出 , adset.name , adset.id
  694. var reg = /(.*)\((.*)\)/g;
  695. var match = reg.exec(fbinfo.adgroup);
  696. if (match != null && match.length >= 3) {
  697. fbinfo.adset = match[1];
  698. fbinfo.adset_id = match[2];
  699. }
  700. // 从 creative 里面提取 ad ad_id
  701. reg = /(.*)\((.*)\)/g;
  702. match = reg.exec(fbinfo.creative);
  703. if (match != null && match.length >= 3) {
  704. fbinfo.ad = match[1];
  705. fbinfo.ad_id = match[2];
  706. }
  707. }
  708. // gg.
  709. if (fbinfo.fbp != undefined && fbinfo.fbp != null && fbinfo.fbp != '' && fbinfo.fbp.indexOf('gg.') >= 0) {
  710. fbinfo.ad = fbinfo.creative;
  711. fbinfo.ad_id = fbinfo.creative;
  712. fbinfo.adset = fbinfo.adgroup;
  713. fbinfo.adset_id = fbinfo.adgroup;
  714. }
  715. }
  716. fbinfo._fbp = fbinfo.fbp;
  717. fbinfo._fbc = fbinfo.fbc;
  718. // if (isNaN(fbinfo.ad_id)){
  719. // fbinfo.ad_id = '';
  720. // }
  721. // if(isNaN(fbinfo.adset_id)){
  722. // fbinfo.adset_id = '';
  723. // }
  724. } catch (e) {
  725. console.log('parse error:', e);
  726. }
  727. bccallback(fbinfo);
  728. });
  729. sureCheck();
  730. }
  731. // 在 fbqreq 事件后面调用
  732. function tryFbBind(userid, uuid, channel, fbclid) {
  733. if (fbclid && fbclid.length > 0) {
  734. return;
  735. }
  736. setTimeout(function () {
  737. var initUrl = dualstackApi + '/api/init-h5-client';
  738. var xhr = new XMLHttpRequest();
  739. xhr.open('POST', initUrl, true);
  740. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  741. var data = 'machine_serial=' + uuid + '&channel=' + channel + '&userid=' + userid;
  742. xhr.send(data);
  743. xhr.onload = function (data) { };
  744. }, 10);
  745. }