/** * Send a page view through Segment * * @param array $page_data The array of page data as specified for the Segment.io PHP library * @param bool $js Set this parameter to true to generate the JS code instead of using PHP. * @param array $js_options The array of options to set for the JS "options" parameter - "integrations" * options get specified in $page_data, but may be overridden here. * @param string $js_callback If you want to use a callback function with "page," specify it here. * * @return mixed True or false depending on if the PHP version succeeded, or the JS code if we compiled the JS code */ public function page(array $page_data = array(), $js = true, array $js_options = array(), $js_callback = null) { // Set the userId or anonymousId if userId is missing. $this->_set_identity($page_data); // Add the context data. $page_data = \Arr::merge($this->_get_context($js), $page_data); if ($js !== true) { // JS sets the defaults on its own, so we do this only for PHP. $page_data = \Arr::merge($this->_get_page_properties(), $page_data); return \Analytics::page($page_data); } else { // Category $js_params[] = !empty($page_data['category']) ? "'" . $page_data['category'] . "'" : "null"; // Name $js_params[] = !empty($page_data['name']) ? "'" . $page_data['name'] . "'" : "null"; // Properties $js_params[] = !empty($page_data['properties']) ? json_encode($page_data['properties']) : "{}"; // Options $js_params[] = $this->_set_js_options($js_options, $page_data); // Callback $js_params[] = !empty($js_callback) ? $js_callback : "null"; $js_output = 'analytics.page(' . implode(',', $js_params) . ');'; /** * To make things easier, developers can skip calling this method without any parameters. * The render() method will always generate the analytics.page() call, so we don't set that * here. * * @todo Find a clean, scalable way to check this. */ if ($js_output !== 'analytics.page(null,null,{},{},null);') { // Add it to the queue. return $this->_js_scripts['page'] = $js_output; } } }