Advertisement
clickio

sql talgat

Feb 13th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.48 KB | None | 0 0
  1. SET @start_date = '2023-01-01';
  2. #SET @end_date = '2023-08-31';
  3. SET @end_date = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
  4. SET @user_id = 151256;
  5.  
  6. DROP TEMPORARY TABLE IF EXISTS
  7. tmp_usd_rate;
  8.  
  9. CREATE TEMPORARY TABLE
  10. tmp_usd_rate
  11. (KEY (`date`))
  12. SELECT
  13. e.`date`,
  14. AVG(e.exrate) AS exrate
  15. FROM
  16. rabota_db.exchange_rate e
  17. WHERE
  18. e.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  19. AND e.source_cur_id = 6
  20. AND e.destination_cur_id = 2
  21. GROUP BY
  22. e.`date`;
  23.  
  24. DROP TEMPORARY TABLE IF EXISTS
  25. tmp_eur_rate;
  26.  
  27. CREATE TEMPORARY TABLE
  28. tmp_eur_rate
  29. (KEY (`date`))
  30. SELECT
  31. e.`date`,
  32. AVG(e.exrate) AS exrate
  33. FROM
  34. rabota_db.exchange_rate e
  35. WHERE
  36. e.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  37. AND e.source_cur_id = 6
  38. AND e.destination_cur_id = 4
  39. GROUP BY
  40. e.`date`;
  41.  
  42. DROP TEMPORARY TABLE IF EXISTS tmp_rub_rate;
  43. CREATE TEMPORARY TABLE tmp_rub_rate (KEY (`date`))
  44. SELECT
  45. e.`date`,
  46. AVG(e.exrate) AS exrate
  47. FROM rabota_db.exchange_rate e
  48. WHERE e.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  49. AND e.source_cur_id = 6
  50. AND e.destination_cur_id = 1
  51. GROUP BY e.`date`;
  52.  
  53. DELETE FROM
  54. tablo.unified_site_data
  55. WHERE
  56. `date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  57. AND user_id = @user_id;
  58.  
  59. #Insert GA
  60. INSERT INTO
  61. tablo.unified_site_data
  62. (
  63. `date`,
  64. account_id,
  65. account_type,
  66. url,
  67. user_id,
  68. site_id,
  69. advertiser_id,
  70. advertiser_name,
  71. device,
  72. sessions,
  73. pageviews,
  74. users,
  75. content_platform
  76. )
  77. SELECT
  78. ga.`date`,
  79. ci.ga_view_id,
  80. 'analytics',
  81. IFNULL(s.url_domain, s.url) AS uri,
  82. s.user_id,
  83. ci.site_id,
  84. 0,
  85. 'Google Analytics',
  86. CONCAT(UPPER(LEFT(ga.device_category, 1)), SUBSTR(ga.device_category, 2)) AS device,
  87. SUM(ga.sessions),
  88. SUM(ga.pageviews),
  89. SUM(ga.users),
  90. IF(data_source='(not set)' OR data_source='web', 'Web', 'AMP') AS content_platform
  91. FROM
  92. imports.`ga_unified_reporting` ga
  93. JOIN rabota_db.ga_custom_import ci
  94. ON ci.ga_import_id = ga.ga_import_id
  95. JOIN rabota_db.site s
  96. ON s.site_id = ci.site_id
  97. WHERE
  98. ga.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  99. AND (ci.field_group='unified_reporting_devices_users')
  100. AND s.`user_id` = @user_id
  101. GROUP BY `date`,ci.ga_view_id,uri,user_id,ci.site_id,device,content_platform;
  102.  
  103. #Insert GA4 AMP
  104. INSERT INTO
  105. tablo.unified_site_data
  106. (
  107. `date`,
  108. account_id,
  109. account_type,
  110. url,
  111. user_id,
  112. site_id,
  113. advertiser_id,
  114. advertiser_name,
  115. device,
  116. sessions,
  117. pageviews,
  118. users,
  119. content_platform
  120. )
  121. SELECT
  122. ga.`date`,
  123. ci.ga_view_id,
  124. 'analytics',
  125. IFNULL(s.url_domain, s.url) AS url,
  126. s.user_id,
  127. ci.site_id,
  128. 0,
  129. 'Google Analytics',
  130. 'Mobile' AS device,
  131. SUM(ga.sessions),
  132. SUM(ga.pageviews),
  133. SUM(ga.users),
  134. 'AMP' AS content_platform
  135. FROM
  136. imports.`ga_unified_reporting` ga
  137. JOIN rabota_db.ga_custom_import ci
  138. ON ci.ga_import_id = ga.ga_import_id
  139. JOIN rabota_db.site s
  140. ON s.site_id = ci.site_id
  141. WHERE
  142. ga.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  143. AND ci.field_group = 'ga4_unified_reporting_no_devices'
  144. AND s.user_id = @user_id
  145. GROUP BY `date`,ci.ga_view_id,url,user_id,ci.site_id,device,content_platform;
  146.  
  147. # for subtracting from total in order to get web
  148. DROP TEMPORARY TABLE IF EXISTS tmp_ga4_amp;
  149. CREATE TEMPORARY TABLE tmp_ga4_amp (KEY (`date`, site_id, device_category))
  150. SELECT
  151. ga.`date`,
  152. ci.site_id,
  153. 'mobile' AS device_category,
  154. SUM(ga.sessions) AS sessions,
  155. SUM(ga.pageviews) AS pageviews,
  156. SUM(ga.users) AS users
  157. FROM
  158. imports.`ga_unified_reporting` ga
  159. JOIN rabota_db.ga_custom_import ci
  160. ON ci.ga_import_id = ga.ga_import_id
  161. JOIN rabota_db.site s
  162. ON s.site_id = ci.site_id
  163. WHERE
  164. ga.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  165. AND ci.field_group = 'ga4_unified_reporting_no_devices'
  166. AND s.user_id = @user_id
  167. GROUP BY `date`, ci.site_id;
  168.  
  169. #Insert GA4 Web
  170. INSERT INTO
  171. tablo.unified_site_data
  172. (
  173. `date`,
  174. account_id,
  175. account_type,
  176. url,
  177. user_id,
  178. site_id,
  179. advertiser_id,
  180. advertiser_name,
  181. device,
  182. sessions,
  183. pageviews,
  184. users,
  185. content_platform
  186. )
  187. SELECT
  188. ga.`date`,
  189. ci.ga_view_id,
  190. 'analytics',
  191. IFNULL(s.url_domain, s.url) AS url,
  192. s.user_id,
  193. ci.site_id,
  194. 0,
  195. 'Google Analytics',
  196. IF(ga.device_category = 'smart tv', 'Desktop', CONCAT(UPPER(LEFT(ga.device_category, 1)), SUBSTR(ga.device_category, 2))) AS device,
  197. SUM(ga.sessions) - SUM(IFNULL(gaamp.sessions, 0)),
  198. SUM(ga.pageviews) - SUM(IFNULL(gaamp.pageviews, 0)),
  199. SUM(ga.users) - SUM(IFNULL(gaamp.users, 0)),
  200. 'Web' AS content_platform
  201. FROM
  202. imports.`ga_unified_reporting` ga
  203. JOIN rabota_db.ga_custom_import ci
  204. ON ci.ga_import_id = ga.ga_import_id
  205. JOIN rabota_db.site s
  206. ON s.site_id = ci.site_id
  207. LEFT JOIN tmp_ga4_amp gaamp
  208. ON ga.date = gaamp.date AND ci.site_id = gaamp.site_id AND ga.device_category = gaamp.device_category
  209. WHERE
  210. ga.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  211. AND ci.field_group = 'ga4_unified_reporting_devices'
  212. AND s.user_id = @user_id
  213. GROUP BY `date`,ci.ga_view_id,url,user_id,ci.site_id,device,content_platform;
  214.  
  215. INSERT INTO
  216. tablo.unified_site_data
  217. (
  218. `date`,
  219. account_id,
  220. account_type,
  221. url,
  222. user_id,
  223. site_id,
  224. advertiser_id,
  225. advertiser_name,
  226. device,
  227. requests,
  228. impressions,
  229. clicks,
  230. pageviews_adsense,
  231. adv_expense_gbp,
  232. partner_gain,
  233. partner_gain_gbp,
  234. content_platform,
  235. viewability_viewed,
  236. viewability_measured,
  237.  
  238. adv_expense_usd,
  239. partner_gain_usd,
  240. adv_expense_eur,
  241. partner_gain_eur,
  242. adv_expense_rub,
  243. partner_gain_rub
  244. )
  245. SELECT
  246. a.`date`,
  247. a.client_id,
  248. 'adsense',
  249. IFNULL(IFNULL(s.url_domain, s.url), a.`domain`),
  250. IFNULL(r.clickio_user_id, s.user_id),
  251. r.clickio_site_id,
  252. 0,
  253. 'Google AdSense',
  254. IF(a.platform_type = 'HighEndMobile', 'Mobile', IF(a.platform_type = 'Tablet', 'Tablet', 'Desktop')),
  255. SUM(a.ad_requests),
  256. SUM(a.impressions),
  257. SUM(a.clicks),
  258. SUM(a.page_views),
  259. SUM(a.earnings_gbp),
  260. SUM(a.earnings),
  261. SUM(a.earnings_gbp),
  262. CASE WHEN a.content_platform = 'Web Story' THEN 'Web'
  263. ELSE a.content_platform END AS content_platform,
  264. SUM(a.measurability * a.impressions * a.viewability),
  265. SUM(a.measurability * a.impressions),
  266.  
  267. ROUND(SUM(a.earnings_gbp) * usd.exrate, 8),
  268. ROUND(SUM(a.earnings_gbp) * usd.exrate, 8),
  269. ROUND(SUM(a.earnings_gbp) * eur.exrate, 8),
  270. ROUND(SUM(a.earnings_gbp) * eur.exrate, 8),
  271. ROUND(SUM(a.earnings_gbp) * rub.exrate, 8),
  272. ROUND(SUM(a.earnings_gbp) * rub.exrate, 8)
  273. FROM
  274. imports.adsense_raw_data a
  275. JOIN tablo.unified_adsense_domain_relation r
  276. ON r.`domain` = a.`domain`
  277. AND r.client_id = a.client_id
  278. LEFT JOIN rabota_db.site s
  279. ON s.site_id = r.clickio_site_id
  280. JOIN tmp_usd_rate usd
  281. ON usd.`date` = a.`date`
  282. JOIN tmp_eur_rate eur
  283. ON eur.`date` = a.`date`
  284. JOIN tmp_rub_rate rub
  285. ON rub.date = a.date
  286. WHERE
  287. a.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  288. AND r.clickio_user_id = @user_id
  289. GROUP BY
  290. a.`date`,
  291. a.client_id,
  292. IFNULL(IFNULL(s.url_domain, s.url), a.`domain`),
  293. r.clickio_site_id,
  294. IF(a.platform_type = 'HighEndMobile', 'Mobile', IF(a.platform_type = 'Tablet', 'Tablet', 'Desktop')),
  295. CASE WHEN a.content_platform = 'Web Story' THEN 'Web'
  296. ELSE a.content_platform END
  297. HAVING
  298. SUM(a.earnings) > 0;
  299.  
  300. # ad manager - by sites
  301.  
  302. INSERT INTO
  303. tablo.unified_site_data
  304. (
  305. `date`,
  306. account_id,
  307. account_type,
  308. url,
  309. user_id,
  310. site_id,
  311. advertiser_id,
  312. advertiser_name,
  313. device,
  314. requests,
  315. impressions,
  316. clicks,
  317. adv_expense_gbp,
  318. partner_gain,
  319. partner_gain_gbp,
  320. content_platform,
  321. adv_expense_usd,
  322. partner_gain_usd,
  323. adv_expense_eur,
  324. partner_gain_eur,
  325. adv_expense_rub,
  326. partner_gain_rub,
  327. demand_channel,
  328. viewability_viewed,
  329. viewability_measured
  330. )
  331. SELECT
  332. a.`date`,
  333. a.network_id,
  334. 'admanager',
  335. IFNULL(IFNULL(s.url_domain, s.url), f.`domain`),
  336. s.user_id,
  337. r.clickio_site_id,
  338. 0,
  339. 'Google Ad Manager',
  340. IF(a.device_category_name = 'Smartphone', 'Mobile', IF(a.device_category_name = 'Tablet', 'Tablet', 'Desktop')),
  341. SUM(a.requests),
  342. SUM(a.impressions),
  343. SUM(a.clicks),
  344. SUM(a.revenue * IFNULL(e.exrate, 1)),
  345. SUM(a.revenue),
  346. SUM(a.revenue * IFNULL(e.exrate, 1)),
  347. IF(a.inventory_types = 'AMP', 'AMP', 'Web'),
  348. ROUND(SUM(a.revenue * IFNULL(e.exrate, 1)) * usd.exrate, 8),
  349. ROUND(SUM(a.revenue * IFNULL(e.exrate, 1)) * usd.exrate, 8),
  350. ROUND(SUM(a.revenue * IFNULL(e.exrate, 1)) * eur.exrate, 8),
  351. ROUND(SUM(a.revenue * IFNULL(e.exrate, 1)) * eur.exrate, 8),
  352. ROUND(SUM(a.revenue * IFNULL(e.exrate, 1)) * rub.exrate, 8),
  353. ROUND(SUM(a.revenue * IFNULL(e.exrate, 1)) * rub.exrate, 8),
  354. a.demand_channel,
  355. SUM(a.viewable_impressions),
  356. SUM(a.measurable_impressions)
  357. FROM
  358. tablo.unified_reporting_gam a
  359. JOIN tablo.unified_reporting_gam_site_filter f
  360. ON a.date = f.date
  361. AND a.ad_unit_id = f.ad_unit_id
  362. AND a.network_id = f.network_id
  363. JOIN tablo.unified_gam_domain_relation r
  364. ON r.`domain` = f.`domain`
  365. AND r.network_id = a.network_id
  366. LEFT JOIN rabota_db.site s
  367. ON s.site_id = r.clickio_site_id
  368. LEFT JOIN rabota_db.exchange_rate e
  369. ON a.date = e.date AND r.currency_id = e.source_cur_id AND e.destination_cur_id = 6 AND e.exchange_rate_source_id = 3
  370. JOIN tmp_usd_rate usd
  371. ON usd.`date` = a.`date`
  372. JOIN tmp_eur_rate eur
  373. ON eur.`date` = a.`date`
  374. JOIN tmp_rub_rate rub
  375. ON rub.date = a.date
  376. WHERE
  377. a.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  378. AND r.clickio_user_id = @user_id
  379. GROUP BY
  380. a.`date`,
  381. a.network_id,
  382. IFNULL(IFNULL(s.url_domain, s.url), f.`domain`),
  383. r.clickio_site_id,
  384. IF(a.device_category_name = 'Smartphone', 'Mobile', IF(a.device_category_name = 'Tablet', 'Tablet', 'Desktop')),
  385. IF(a.inventory_types = 'AMP', 'AMP', 'Web'),
  386. a.demand_channel
  387. #HAVING
  388. # SUM(a.revenue) > 0
  389. ;
  390.  
  391. DROP TEMPORARY TABLE IF EXISTS
  392. tmp_sites_dates;
  393.  
  394. CREATE TEMPORARY TABLE
  395. tmp_sites_dates
  396. (KEY (site_id, `date`))
  397. SELECT DISTINCT
  398. site_id,
  399. `date`
  400. FROM
  401. tablo.unified_site_data
  402. WHERE
  403. `date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  404. AND NOT site_id IS NULL
  405. AND user_id = @user_id;
  406.  
  407. INSERT INTO
  408. tablo.unified_site_data
  409. (
  410. `date`,
  411. account_id,
  412. account_type,
  413. url,
  414. user_id,
  415. site_id,
  416. advertiser_id,
  417. device,
  418. requests,
  419. impressions,
  420. clicks,
  421. adv_expense_gbp,
  422. partner_gain,
  423. partner_gain_gbp,
  424. content_platform,
  425. viewability_viewed,
  426. viewability_measured,
  427. external_view_count,
  428.  
  429. adv_expense_usd,
  430. partner_gain_usd,
  431. adv_expense_eur,
  432. partner_gain_eur,
  433. adv_expense_rub,
  434. partner_gain_rub,
  435.  
  436. pub_adserver_cost,
  437. pub_adserver_cost_gbp,
  438. company_adserver_cost,
  439. company_adserver_cost_gbp
  440. )
  441. SELECT
  442. npm.`date`,
  443. s.user_id,
  444. 'clickio',
  445. IFNULL(s.url_domain, s.url),
  446. s.user_id,
  447. npm.site_id,
  448. npm.adv_net_id,
  449. IF(npm.device_type IN ('High-end mobile devices', 'Smartphone'), 'Mobile', IF(npm.device_type IN ('Tablets', 'Tablet'), 'Tablet', 'Desktop')),
  450. SUM(npm.external_first_request_count),
  451. SUM(npm.external_first_view_count),
  452. SUM(npm.hit_count),
  453. SUM(npm.adv_expense_gbp),
  454. SUM(npm.partner_gain),
  455. SUM(npm.partner_gain_gbp),
  456. CASE WHEN sd24.is_amp = 1 THEN 'AMP' ELSE 'Web' END,
  457. SUM(npm.external_viewability_viewed_impressions),
  458. SUM(npm.external_viewability_measured_impressions),
  459. SUM(npm.external_view_count),
  460.  
  461. ROUND(SUM(npm.adv_expense_gbp) * usd.exrate, 8),
  462. ROUND(SUM(npm.partner_gain_gbp) * usd.exrate, 8),
  463. ROUND(SUM(npm.adv_expense_gbp) * eur.exrate, 8),
  464. ROUND(SUM(npm.partner_gain_gbp) * eur.exrate, 8),
  465. ROUND(SUM(npm.adv_expense_gbp) * rub.exrate, 8),
  466. ROUND(SUM(npm.partner_gain_gbp) * rub.exrate, 8),
  467.  
  468. SUM(npm.`pub_adserver_cost`) AS pub_adserver_cost,
  469. SUM(npm.`pub_adserver_cost_gbp`) AS pub_adserver_cost_gbp,
  470. SUM(npm.`company_adserver_cost`) AS company_adserver_cost,
  471. SUM(npm.`company_adserver_cost_gbp`) AS company_adserver_cost_gbp
  472. FROM
  473. tablo.npm_site_area_device_stat npm
  474. JOIN tmp_sites_dates tmp
  475. ON tmp.site_id = npm.site_id
  476. AND tmp.`date` = npm.`date`
  477. AND npm.site_id <> 230029
  478. JOIN rabota_db.site s
  479. ON s.site_id = npm.site_id
  480. JOIN rabota_db.site_area_design_24 sd24
  481. ON sd24.site_area_id = npm.site_area_id
  482. JOIN tmp_usd_rate usd
  483. ON usd.`date` = npm.`date`
  484. JOIN tmp_eur_rate eur
  485. ON eur.`date` = npm.`date`
  486. JOIN tmp_rub_rate rub
  487. ON rub.date = npm.date
  488. GROUP BY
  489. npm.`date`,
  490. IFNULL(s.url_domain, s.url),
  491. npm.site_id,
  492. npm.adv_net_id,
  493. IF(npm.device_type IN ('High-end mobile devices', 'Smartphone'), 'Mobile', IF(npm.device_type IN ('Tablets', 'Tablet'), 'Tablet', 'Desktop')),
  494. CASE WHEN sd24.is_amp = 1 THEN 'AMP' ELSE 'Web' END
  495. # HAVING
  496. # SUM(npm.partner_gain) > 0
  497. # for inclusion of Direct Campaigns (cmf_id: 123)
  498. ;
  499.  
  500. UPDATE
  501. tablo.unified_site_data u
  502. LEFT JOIN rabota_db.demand_source d
  503. ON d.cmf_system_id = u.advertiser_id
  504. SET
  505. u.advertiser_name = IFNULL(d.name, 'Unknown')
  506. WHERE
  507. u.account_type = 'clickio'
  508. AND u.date BETWEEN DATE(@start_date) AND DATE(@end_date)
  509. AND u.`user_id` = @user_id;
  510.  
  511. DELETE FROM
  512. tablo.unified_ad_unit_data
  513. WHERE
  514. `date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  515. AND user_id = @user_id;
  516.  
  517. INSERT INTO
  518. tablo.unified_ad_unit_data
  519. (
  520. `date`,
  521. account_id,
  522. account_type,
  523. url,
  524. user_id,
  525. site_id,
  526. ad_unit_id,
  527. ad_unit_name,
  528. advertiser_id,
  529. advertiser_name,
  530. device,
  531. requests,
  532. impressions,
  533. clicks,
  534. adv_expense_gbp,
  535. partner_gain,
  536. partner_gain_gbp,
  537. content_platform,
  538. viewability_viewed,
  539. viewability_measured,
  540. adsense_ad_format
  541. )
  542. SELECT
  543. a.`date`,
  544. a.client_id,
  545. 'adsense',
  546. IFNULL(IFNULL(s.url_domain, s.url), a.`domain`),
  547. IFNULL(r.clickio_user_id, s.user_id),
  548. r.clickio_site_id,
  549. a.unit_id,
  550. a.unit_name,
  551. 0,
  552. 'Google AdSense',
  553. IF(a.platform_type = 'HighEndMobile', 'Mobile', IF(a.platform_type = 'Tablet', 'Tablet', 'Desktop')),
  554. SUM(a.ad_requests),
  555. SUM(a.impressions),
  556. SUM(a.clicks),
  557. SUM(a.earnings_gbp),
  558. SUM(a.earnings),
  559. SUM(a.earnings_gbp),
  560. CASE WHEN a.content_platform = 'Web Story' THEN 'Web'
  561. ELSE a.content_platform END AS content_platform,
  562. SUM(a.measurability * a.impressions * a.viewability),
  563. SUM(a.measurability * a.impressions),
  564. 'In-page'
  565. FROM
  566. imports.adsense_adunit_raw_data a
  567. JOIN tablo.unified_adsense_domain_relation r
  568. ON r.`domain` = a.`domain`
  569. AND r.client_id = a.client_id
  570. LEFT JOIN rabota_db.site s
  571. ON s.site_id = r.clickio_site_id
  572. WHERE
  573. a.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  574. AND r.clickio_user_id = @user_id
  575. GROUP BY
  576. a.`date`,
  577. a.client_id,
  578. IFNULL(IFNULL(s.url_domain, s.url), a.`domain`),
  579. r.clickio_site_id,
  580. a.unit_id,
  581. IF(a.platform_type = 'HighEndMobile', 'Mobile', IF(a.platform_type = 'Tablet', 'Tablet', 'Desktop')),
  582. CASE WHEN a.content_platform = 'Web Story' THEN 'Web'
  583. ELSE a.content_platform END
  584. HAVING
  585. SUM(a.earnings) > 0;
  586.  
  587. INSERT INTO
  588. tablo.unified_ad_unit_data
  589. (
  590. `date`,
  591. account_id,
  592. account_type,
  593. url,
  594. user_id,
  595. site_id,
  596. ad_unit_id,
  597. ad_unit_name,
  598. advertiser_id,
  599. advertiser_name,
  600. device,
  601. requests,
  602. impressions,
  603. clicks,
  604. adv_expense_gbp,
  605. partner_gain,
  606. partner_gain_gbp,
  607. content_platform,
  608. demand_channel,
  609. viewability_viewed,
  610. viewability_measured
  611. )
  612. SELECT
  613. a.`date`,
  614. a.network_id,
  615. 'admanager',
  616. IFNULL(IFNULL(s.url_domain, s.url), f.`domain`),
  617. s.user_id,
  618. r.clickio_site_id,
  619. a.ad_unit_id,
  620. a.ad_unit_name,
  621. 0,
  622. 'Google Ad Manager',
  623. IF(a.device_category_name = 'Smartphone', 'Mobile', IF(a.device_category_name = 'Tablet', 'Tablet', 'Desktop')),
  624. SUM(a.requests),
  625. SUM(a.impressions),
  626. SUM(a.clicks),
  627. SUM(a.revenue * IFNULL(e.exrate, 1)),
  628. SUM(a.revenue),
  629. SUM(a.revenue * IFNULL(e.exrate, 1)),
  630. IF(a.inventory_types = 'AMP', 'AMP', 'Web'),
  631. a.demand_channel,
  632. SUM(a.viewable_impressions),
  633. SUM(a.measurable_impressions)
  634. FROM
  635. tablo.unified_reporting_gam a
  636. JOIN tablo.unified_reporting_gam_site_filter f
  637. ON a.date = f.date
  638. AND a.ad_unit_id = f.ad_unit_id
  639. AND a.network_id = f.network_id
  640. JOIN tablo.unified_gam_domain_relation r
  641. ON r.`domain` = f.`domain`
  642. AND r.network_id = a.network_id
  643. LEFT JOIN rabota_db.site s
  644. ON s.site_id = r.clickio_site_id
  645. LEFT JOIN rabota_db.exchange_rate e
  646. ON a.date = e.date AND r.currency_id = e.source_cur_id AND e.destination_cur_id = 6 AND e.exchange_rate_source_id = 3
  647. WHERE
  648. a.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  649. AND r.clickio_user_id = @user_id
  650. GROUP BY
  651. a.`date`,
  652. a.network_id,
  653. IFNULL(IFNULL(s.url_domain, s.url), f.`domain`),
  654. r.clickio_site_id,
  655. a.ad_unit_id,
  656. IF(a.device_category_name = 'Smartphone', 'Mobile', IF(a.device_category_name = 'Tablet', 'Tablet', 'Desktop')),
  657. IF(a.inventory_types = 'AMP', 'AMP', 'Web'),
  658. a.demand_channel
  659. #HAVING
  660. # SUM(a.revenue) > 0
  661. ;
  662.  
  663. INSERT INTO
  664. tablo.unified_ad_unit_data
  665. (
  666. `date`,
  667. account_id,
  668. account_type,
  669. url,
  670. user_id,
  671. site_id,
  672. ad_unit_id,
  673. advertiser_id,
  674. device,
  675. requests,
  676. impressions,
  677. clicks,
  678. adv_expense_gbp,
  679. partner_gain,
  680. partner_gain_gbp,
  681. content_platform,
  682. viewability_viewed,
  683. viewability_measured,
  684. external_view_count,
  685.  
  686. pub_adserver_cost,
  687. pub_adserver_cost_gbp,
  688. company_adserver_cost,
  689. company_adserver_cost_gbp
  690. )
  691. SELECT
  692. npm.`date`,
  693. s.user_id,
  694. 'clickio',
  695. IFNULL(s.url_domain, s.url),
  696. s.user_id,
  697. npm.site_id,
  698. IFNULL(asa.site_area_id, npm.site_area_id),
  699. npm.adv_net_id,
  700. IF(npm.device_type IN ('High-end mobile devices', 'Smartphone'), 'Mobile', IF(npm.device_type IN ('Tablets', 'Tablet'), 'Tablet', 'Desktop')),
  701. SUM(npm.external_first_request_count),
  702. SUM(npm.external_first_view_count),
  703. SUM(npm.hit_count),
  704. SUM(npm.adv_expense_gbp),
  705. SUM(npm.partner_gain),
  706. SUM(npm.partner_gain_gbp),
  707. CASE WHEN sd24.is_amp = 1 THEN 'AMP' ELSE 'Web' END,
  708. SUM(npm.external_viewability_viewed_impressions),
  709. SUM(npm.external_viewability_measured_impressions),
  710. SUM(npm.external_view_count),
  711.  
  712. SUM(npm.`pub_adserver_cost`) AS pub_adserver_cost,
  713. SUM(npm.`pub_adserver_cost_gbp`) AS pub_adserver_cost_gbp,
  714. SUM(npm.`company_adserver_cost`) AS company_adserver_cost,
  715. SUM(npm.`company_adserver_cost_gbp`) AS company_adserver_cost_gbp
  716. FROM
  717. tablo.npm_site_area_device_stat npm
  718. JOIN tmp_sites_dates tmp
  719. ON tmp.site_id = npm.site_id
  720. AND tmp.`date` = npm.`date`
  721. AND npm.site_id <> 230029
  722. LEFT JOIN rabota_db.attached_site_areas asa
  723. ON asa.attached_site_area_id = npm.site_area_id
  724. JOIN rabota_db.site s
  725. ON s.site_id = npm.site_id
  726. JOIN rabota_db.site_area_design_24 sd24
  727. ON sd24.site_area_id = IFNULL(asa.site_area_id, npm.site_area_id)
  728. GROUP BY
  729. npm.`date`,
  730. IFNULL(s.url_domain, s.url),
  731. npm.site_id,
  732. IFNULL(asa.site_area_id, npm.site_area_id),
  733. npm.adv_net_id,
  734. IF(npm.device_type IN ('High-end mobile devices', 'Smartphone'), 'Mobile', IF(npm.device_type IN ('Tablets', 'Tablet'), 'Tablet', 'Desktop')),
  735. CASE WHEN sd24.is_amp = 1 THEN 'AMP' ELSE 'Web' END
  736. # HAVING
  737. # SUM(npm.partner_gain) > 0
  738. # for inclusion of Direct Campaigns (cmf_id: 123)
  739. ;
  740.  
  741. UPDATE
  742. tablo.unified_ad_unit_data u
  743. LEFT JOIN rabota_db.demand_source d
  744. ON d.cmf_system_id = u.advertiser_id
  745. SET
  746. u.advertiser_name = IFNULL(d.name, 'Unknown')
  747. WHERE
  748. u.account_type = 'clickio'
  749. AND u.date BETWEEN DATE(@start_date) AND DATE(@end_date)
  750. AND u.`user_id` = @user_id;
  751.  
  752. UPDATE
  753. tablo.unified_ad_unit_data u
  754. LEFT JOIN rabota_db.site_area sa
  755. ON sa.site_area_id = u.ad_unit_id
  756. SET
  757. u.ad_unit_name = IFNULL(sa.name, 'no_ad_unit_name')
  758. WHERE
  759. u.account_type = 'clickio'
  760. AND u.date BETWEEN DATE(@start_date) AND DATE(@end_date)
  761. AND u.`user_id` = @user_id;
  762.  
  763. INSERT INTO
  764. tablo.unified_ad_unit_data
  765. (
  766. `date`,
  767. account_id,
  768. account_type,
  769. url,
  770. user_id,
  771. site_id,
  772. ad_unit_id,
  773. ad_unit_name,
  774. advertiser_id,
  775. advertiser_name,
  776. device,
  777. requests,
  778. impressions,
  779. clicks,
  780. adv_expense_gbp,
  781. partner_gain,
  782. partner_gain_gbp,
  783. content_platform,
  784. adsense_ad_format,
  785. viewability_viewed,
  786. viewability_measured
  787. )
  788. SELECT
  789. a.`date`,
  790. a.client_id,
  791. 'adsense',
  792. IFNULL(IFNULL(s.url_domain, s.url), a.`domain`),
  793. IFNULL(r.clickio_user_id, s.user_id),
  794. r.clickio_site_id,
  795. 0,
  796. '[auto ads]',
  797. 0,
  798. 'Google AdSense',
  799. IF(a.platform_type = 'HighEndMobile', 'Mobile', IF(a.platform_type = 'Tablet', 'Tablet', 'Desktop')),
  800. SUM(a.ad_requests),
  801. SUM(a.impressions),
  802. SUM(a.clicks),
  803. SUM(a.earnings_gbp),
  804. SUM(a.earnings),
  805. SUM(a.earnings_gbp),
  806. CASE WHEN a.content_platform = 'Web Story' THEN 'Web'
  807. ELSE a.content_platform END AS content_platform,
  808. a.ad_format_name,
  809. SUM(a.measurability * a.impressions * a.viewability),
  810. SUM(a.measurability * a.impressions)
  811. FROM
  812. imports.`adsense_raw_data` a
  813. JOIN tablo.unified_adsense_domain_relation r
  814. ON r.`domain` = a.`domain`
  815. AND r.client_id = a.client_id
  816. LEFT JOIN rabota_db.site s
  817. ON s.site_id = r.clickio_site_id
  818. WHERE
  819. a.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  820. AND a.ad_placement_name = 'Auto ads'
  821. AND r.clickio_user_id = @user_id
  822. GROUP BY
  823. a.`date`,
  824. a.client_id,
  825. IFNULL(IFNULL(s.url_domain, s.url), a.`domain`),
  826. r.clickio_site_id,
  827. IF(a.platform_type = 'HighEndMobile', 'Mobile', IF(a.platform_type = 'Tablet', 'Tablet', 'Desktop')),
  828. CASE WHEN a.content_platform = 'Web Story' THEN 'Web'
  829. ELSE a.content_platform END,
  830. a.ad_format_name
  831. HAVING
  832. SUM(a.earnings) > 0;
  833.  
  834. #update site_data adsense access
  835. DROP TEMPORARY TABLE IF EXISTS tmp_mcm_access;
  836. CREATE TEMPORARY TABLE tmp_mcm_access(KEY(account_id))
  837. SELECT
  838. CONCAT('ca-', child_network_id) AS account_id
  839. FROM rabota_db.`mcm_publisher_date_settings`
  840. WHERE mcm_type = 'adsense'
  841. ;
  842.  
  843. #active API access
  844. DROP TEMPORARY TABLE IF EXISTS tmp_active_api_access;
  845. CREATE TEMPORARY TABLE tmp_active_api_access (KEY(account_id))
  846. SELECT CONCAT("ca-", network_id) AS account_id,
  847. google_user_settings_id
  848. FROM rabota_db.`adsense_custom_import`
  849. WHERE
  850. google_user_settings_id NOT IN (1, 2, 3, 6, 15, 84, 9, 72, 148, 197, 463)
  851. AND last_import_status = 'OK'
  852. AND today_every_minute IS NOT NULL
  853. GROUP BY CONCAT("ca-", network_id)
  854. ;
  855.  
  856. # set area_type
  857. UPDATE tablo.unified_ad_unit_data a
  858. JOIN rabota_db.site_area_type sat ON sat.site_area_id = a.ad_unit_id
  859. SET a.area_type = sat.`site_area_type_pub`
  860. WHERE a.`account_type` = 'clickio' AND a.`date` BETWEEN DATE(@start_date) AND DATE(@end_date)
  861. AND a.user_id = @user_id;
  862.  
  863. #Adsense null user_id
  864. DROP TEMPORARY TABLE IF EXISTS tmp_adsense_null;
  865. CREATE TEMPORARY TABLE tmp_adsense_null (KEY(client_id, user_id))
  866. SELECT
  867. a.client_id,
  868. b.user_id
  869. FROM tablo.`unified_adsense_domain_relation` a
  870. JOIN rabota_db.`site` b
  871. ON a.clickio_site_id = b.site_id
  872. GROUP BY a.client_id;
  873.  
  874. UPDATE tablo.`unified_site_data` a
  875. JOIN tmp_adsense_null b
  876. ON a.`account_id` = b.client_id
  877. SET a.`user_id` = b.user_id
  878. WHERE a.account_type = 'adsense' AND a.`user_id` IS NULL
  879. AND a.`date` BETWEEN @start_date AND @end_date
  880. AND a.user_id = @user_id;
  881.  
  882. UPDATE tablo.`unified_ad_unit_data` a
  883. JOIN tmp_adsense_null b
  884. ON a.`account_id` = b.client_id
  885. SET a.`user_id` = b.user_id
  886. WHERE a.account_type = 'adsense' AND a.`user_id` IS NULL
  887. AND a.`date` BETWEEN @start_date AND @end_date
  888. AND a.`user_id` = @user_id;
  889.  
  890. #Adsense account name
  891. DROP TEMPORARY TABLE IF EXISTS tmp_adsense_name;
  892. CREATE TEMPORARY TABLE tmp_adsense_name (KEY(client_id))
  893. SELECT
  894. a.client_id,
  895. a.account_name
  896. FROM imports.`adsense_raw_data` a
  897. WHERE DATE >= CURDATE() - INTERVAL 30 DAY
  898. AND a.account_name IS NOT NULL
  899. GROUP BY a.client_id;
  900.  
  901. UPDATE tablo.`unified_site_data` a
  902. JOIN tmp_adsense_name b
  903. ON a.`account_id` = b.client_id
  904. SET a.`adsense_account` = b.account_name
  905. WHERE account_type = 'adsense'
  906. AND a.`date` BETWEEN @start_date AND @end_date
  907. AND a.`user_id` = @user_id;
  908.  
  909. UPDATE tablo.`unified_ad_unit_data` a
  910. JOIN tmp_adsense_name b
  911. ON a.`account_id` = b.client_id
  912. SET a.`adsense_account` = b.account_name
  913. WHERE account_type = 'adsense'
  914. AND a.`date` BETWEEN @start_date AND @end_date
  915. AND a.`user_id` = @user_id;
  916.  
  917. #Commercial region and publisher currency
  918. DROP TEMPORARY TABLE IF EXISTS tmp_region_cur;
  919. CREATE TEMPORARY TABLE tmp_region_cur (KEY(user_id))
  920. SELECT
  921. a.user_id,
  922. a.commercial_region,
  923. b.code
  924. FROM rabota_db.`user` a
  925. JOIN rabota_db.`currency` b
  926. ON a.cur_id = b.currency_id;
  927.  
  928. UPDATE tablo.`unified_site_data` a
  929. JOIN tmp_region_cur b
  930. ON a.`user_id` = b.user_id
  931. SET
  932. a.`commercial_region` = b.commercial_region,
  933. a.`currency` = b.code
  934. WHERE a.`date` BETWEEN @start_date AND @end_date
  935. AND a.`user_id` = @user_id;
  936.  
  937. UPDATE tablo.`unified_ad_unit_data` a
  938. JOIN tmp_region_cur b
  939. ON a.`user_id` = b.user_id
  940. SET
  941. a.`commercial_region` = b.commercial_region,
  942. a.`currency` = b.code
  943. WHERE a.`date` BETWEEN @start_date AND @end_date
  944. AND a.`user_id` = @user_id;
  945.  
  946. # site category
  947. UPDATE tablo.unified_site_data u
  948. JOIN rabota_db.site s ON s.site_id = u.site_id
  949. JOIN rabota_db.site_category c ON s.category_id = c.category_id
  950. SET u.site_category = IFNULL(c.name, 'Unknown')
  951. WHERE u.date BETWEEN @start_date AND @end_date
  952. AND u.`user_id` = @user_id;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement