<?php

        $trackingSolutionConfigurations = array (
			TRACKING_SERVICE_UNKNOWN => array (
				'name' => 'Unknown',
				'active' => false,
                'clickPhase' => array (
                    's2s' => false,
                ),
                'completionPhase' => array (
                    's2s' => false,
                )
			),
			TRACKING_SERVICE_S2S => array (
				'name' => 'Server2Server',
				'active' => true,
				'regex' => '/.*/',
				'regexDynamicKey' => 'dynamicParameterSyntax',
                'clickPhase' => array (
                    's2s' => false,
                ),
                'completionPhase' => array (
                    's2s' => true,
                )
			),
			TRACKING_SERVICE_SUPERSONIC_SDK => array (
				'name' => 'Supersonic Tracking SDK',
				'active' => true,
				'notManaged' => true,
                'clickPhase' => array (
                    's2s' => false,
                ),
                'completionPhase' => array (
                    's2s' => false,
                )
            ),
			TRACKING_SERVICE_PIXEL => array (
				'name' => 'Pixel Tracking',
				'active' => false,
                'clickPhase' => array (
                    's2s' => false,
                ),
                'completionPhase' => array (
                    's2s' => false,
                )
			),
            TRACKING_SERVICE_AD_EVEN => array( // Adjust
                'name' => 'AdEven/adjust.io',
				'active' => true,
				'regex' => '/^http[s]?:\/\/app\.adjust\.(io|com)\/.*$/', // if you make changes in this regex, note that changes may be required in the sanitization of this url
                'clickPhase' => array (
                    's2s' => true,
                    'params' => '&idfa={idfa}&gps_adid={aid}&s2s=1',
                ),
                'completionPhase' => array (
                    's2s' => true,
                )
            ),
			TRACKING_SERVICE_AD_X => array(
				'name' => 'Ad-X',
				'active' => true,
				'regex' => '/^http[s]?:\/\/ad-x\.co\.uk\/API\/click\/.*\/NET\/{DynamicParameter}___[0-9]*\/SupersonicAds\?.*$/', //'/^http[s]?:\/\/ad-x\.co\.uk\/API\/click.*$/'
				'regexDynamicKey' => 'id',
                'clickPhase' => array (
                    's2s' => true,
                    'params' => '&redirect=off&ipaddress={ip}',
                    'deliveryToggle' => true, // temp configuration
                ),
                'completionPhase' => array (
                    's2s' => true,
                )
			),
            TRACKING_SERVICE_APPS_FLYER => array(
                'name' => 'AppsFlyer',
				'active' => true,
                'regex' => '/^(?=.*\bclickid={DynamicParameter}(&|$))(?=.*\bpid=supersonicads_int(&|$))http[s]?:\/\/app\.appsflyer\.com\/[^?]+\?(?:[^&]+)(?:&[^&]+)*$/',
                'clickPhase' => array (
                    's2s' => true,
                    'params' => '&redirect=false&idfa={idfa}&advertising_id={aid}&af_cost_model={CostModel}&af_cost_value={CostModelValue}&af_cost_currency=USD',
                ),
                'completionPhase' => array (
                    's2s' => true,
                    '2ndCallbackParams' => array('app_id' => '','authentication' => ''), // Publisher perspective
                )
            ),
			TRACKING_SERVICE_AP_SALAR => array(
				'name' => 'ApSalar',
				'active' => false,
				'regex' => '/^http[s]?:\/\/ad\.apsalar\.com\/api\/v1\/.*$/',
                'clickPhase' => array (
                    's2s' => true,
                    'params' => '&redirect=false',
                    'deliveryToggle' => true, // temp configuration
                ),
                'completionPhase' => array (
                    's2s' => true,
                )
			),
			TRACKING_SERVICE_BBB => array(
				'name' => 'BigBlueBubble',
				'active' => false,
				'regex' => '/^http[s]?:\/\/ua\.bbbgame\.net\/api\/msm\/track\?event=click.*$/',
				'clickPhase' => array (
					's2s' => true,
					'params' => '&redirect=0',
					'deliveryToggle' => true, // temp configuration
				),
				'completionPhase' => array (
					's2s' => true,
				)
			),
            TRACKING_SERVICE_KOCHAVA => array(
                'name' => 'Kochava',
				'active' => true,
                'regex' => '/^http[s]?:\/\/control\.kochava\.com\/[^?]+\?(([^&]+)&)*click_id={DynamicParameter}(&([^&]+))*$/',
                'clickPhase' => array (
                    's2s' => true,
                    'params' => '&pbr=1&device_ua={ua}&ip_address={ip}',
                    'deliveryToggle' => true, // temp configuration
                ),
                'completionPhase' => array (
                    's2s' => true,
                )
            ),
			TRACKING_SERVICE_TAP_STREAM => array(
                'name' => 'Tapstream',
				'active' => false,
                'regex' => '/^http[s]?:\/\/(api\.)?taps\.io\/.*$/',
				'clickPhase' => array (
					's2s' => true,
					'params' => '&__tsip={ip}&__tsredirect=0&__tshardware-android-advertising-id={aid}',
					'deliveryToggle' => true, // temp configuration
				),
				'completionPhase' => array (
					's2s' => true,
				),
            ),
			TRACKING_SERVICE_TENJIN  => array(
				'name' => 'Tenjin',
				'active' => true,
				'regex' => '/^https:\/\/track\.tenjin\.io.+click\/[^?]*\?(([^&]+)&)*((remote_click_id={DynamicParameter}&(([^&]+)&)*advertising_id={DeviceIds\[(IFA|AID)\]})|(advertising_id={DeviceIds\[(IFA|AID)\]}&(([^&]+)&)*remote_click_id={DynamicParameter}))/',
                'clickPhase' => array (
                    's2s' => false,
                ),
                'completionPhase' => array (
                    's2s' => true,
                )
			),
			TRACKING_SERVICE_TUNE => array( // Tune
				'name' => 'Tune',
				'active' => true,
				'regex' => '/^(?=.*\baction=click(&|$))(?=.*\bpublisher_id=[\d]+(&|$))(?=.*\bsite_id=[\d]+(&|$))(?=.*\bref_id={DynamicParameter}(&|$))http[s]?:\/\/(?:(?:[\d]+\.api-[\d]{2})|(?:hastrk[\d]+)|(?:[\d]+\.measurementapi))\.com\/serve\?(?:[^&]+)(?:&[^&]+)*$/',
                'impressionPhase' => array (
                    'params' => '&device_ip={ip}&response_format=json',
                ),
                'clickPhase' => array (
                    's2s' => true,
                    'params' => '&google_aid={aid}&ios_ifa={ifa}&device_ip={ip}&response_format=json',
                    'useEncryption' => true, // Because encryption is on - no CostModel place holders
                ),
                'completionPhase' => array (
                    's2s' => true,
                ),
			),
        );


		$trackingServices = $trackingSolutionConfigurations;
		$inactiveTrackingServices = array();

		foreach ($trackingServices as $serviceId => &$serviceData) {
            // Only when a tracking service is not managed by advertiser, the flag is being checked
			$managedAdvertiserLogic = !isset($serviceData['notManaged']) ? true :
				!true && $serviceData['notManaged'];

			$isActiveTracking = $serviceData['active'] && $managedAdvertiserLogic;
			$serviceData['id'] = $serviceId;
			unset($serviceData['active']);
			unset($serviceData['notManaged']);

			if (!$isActiveTracking){
				unset($trackingServices[$serviceId]);
				$inactiveTrackingServices[$serviceId] = $serviceData;
			}
		}

		if ($onlyActive) {
			print_r($trackingServices);
		} else{
			print_r(array('trackingServices' => $trackingServices, 'inactiveTrackingServices' => $inactiveTrackingServices));
		}


