measurement-protocol.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. function monsterinsights_get_mp_api_url( ) {
  6. if ( monsterinsights_is_debug_mode() ) {
  7. return 'https://www.google-analytics.com/debug/collect';
  8. } else {
  9. return 'https://www.google-analytics.com/collect';
  10. }
  11. }
  12. function monsterinsights_mp_api_call( $args = array() ) {
  13. $user_agent = '';
  14. if ( ! empty( $args['user-agent'] ) ) {
  15. $user_agent = $args['user-agent'];
  16. unset( $args['user-agent'] );
  17. }
  18. $payment_id = 0;
  19. if ( ! empty( $args['payment_id'] ) ) {
  20. $payment_id = $args['payment_id'];
  21. unset( $args['payment_id'] );
  22. }
  23. $defaults = array(
  24. 't' => 'event', // Required: Hit type
  25. 'ec' => '', // Optional: Event category
  26. 'ea' => '', // Optional: Event Action
  27. 'el' => '', // Optional: Event Label
  28. 'ev' => null, // Optional: Event Value
  29. );
  30. $body = array_merge( $defaults , $args );
  31. // We want to get the user's IP address when possible
  32. $ip = '';
  33. if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) && ! filter_var( $_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP ) === false ) {
  34. $ip = $_SERVER['HTTP_CLIENT_IP'];
  35. } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) && ! filter_var( $_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP ) === false ) {
  36. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  37. } else {
  38. $ip = $_SERVER['REMOTE_ADDR'];
  39. }
  40. $ip = apply_filters( 'monsterinsights_mp_api_call_ip', $ip );
  41. // If possible, let's get the user's language
  42. $user_language = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? explode( ',', $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) : array();
  43. $user_language = reset( $user_language );
  44. $user_language = sanitize_text_field( $user_language );
  45. $default_body = array(
  46. // Required: Version
  47. 'v' => '1',
  48. // Required: UA code
  49. 'tid' => monsterinsights_get_ua_to_output( array( 'ecommerce' => $args ) ),
  50. // Required: User visitor ID
  51. 'cid' => monsterinsights_get_client_id( $payment_id ),
  52. // Required: Type of hit (either pageview or event)
  53. 't' => 'pageview', // Required - Hit type
  54. // Optional: Was the event a non-interaction event (for bounce purposes)
  55. 'ni' => true,
  56. // Optional: Document Host Name
  57. 'dh' => str_replace( array( 'http://', 'https://' ), '', site_url() ),
  58. // Optional: Requested URI
  59. 'dp' => $_SERVER['REQUEST_URI'],
  60. // Optional: Page Title
  61. 'dt' => get_the_title(),
  62. // Optional: User language
  63. 'ul' => $user_language,
  64. // Optional: User IP address
  65. 'uip' => $ip,
  66. // Optional: User Agent
  67. 'ua' => ! empty( $user_agent ) ? $user_agent : $_SERVER['HTTP_USER_AGENT'],
  68. // Optional: Time of the event
  69. 'z' => time()
  70. );
  71. $body = wp_parse_args( $body, $default_body );
  72. $body = apply_filters( 'monsterinsights_mp_api_call', $body );
  73. // Ensure that the CID is not empty
  74. if ( empty( $body['cid'] ) ) {
  75. $body['cid'] = monsterinsights_generate_uuid();
  76. }
  77. // Unset empty values to reduce request size
  78. foreach ( $body as $key => $value ) {
  79. if ( empty( $value ) ) {
  80. unset( $body[ $key ] );
  81. }
  82. }
  83. $debug_mode = monsterinsights_is_debug_mode();
  84. $args = array(
  85. 'method' => 'POST',
  86. 'timeout' => '5',
  87. 'blocking' => ( $debug_mode ) ? true : false,
  88. 'body' => $body,
  89. );
  90. if ( ! empty( $user_agent ) ) {
  91. $args['user-agent'] = $user_agent;
  92. }
  93. $response = wp_remote_post( monsterinsights_get_mp_api_url(), $args );
  94. //
  95. //if ( $debug_mode ) {
  96. // monsterinsights_debug_output( $body );
  97. // monsterinsights_debug_output( $response );
  98. //}
  99. return $response;
  100. }
  101. function monsterinsights_mp_track_event_call( $args = array() ) {
  102. $default_args = array(
  103. // Change the default type to event
  104. 't' => 'event',
  105. // Required: Event Category
  106. 'ec' => '',
  107. // Required: Event Action
  108. 'ea' => '',
  109. // Required: Event Label
  110. 'el' => '',
  111. // Optional: Event Value
  112. 'ev' => null,
  113. );
  114. $args = wp_parse_args( $args, $default_args );
  115. //$args = apply_filters( 'monsterinsights_mp_track_event_call', $args );
  116. return monsterinsights_mp_api_call( $args );
  117. }