pixel_h5.js 23 KB

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