/** * assembles an url for tracking measurement without javascript * * e.g. for tracking email open events within a newsletter * * @param string $metricName * @param mixed $metricValue * @param \Ipunkt\LaravelAnalytics\Data\Event $event * @param \Ipunkt\LaravelAnalytics\Data\Campaign $campaign * @param string|null $clientId * @param array $params * * @return string * * @experimental */ public function trackMeasurementUrl($metricName, $metricValue, Event $event, Campaign $campaign, $clientId = null, array $params = []) { $uniqueId = $clientId !== null ? $clientId : uniqid('track_'); if ($event->getLabel() === '') { $event->setLabel($uniqueId); } if ($campaign->getName() === '') { $campaign->setName('Campaign ' . date('Y-m-d')); } $protocol = $this->secureTrackingUrl ? 'https' : 'http'; $defaults = ['url' => $protocol . '://www.google-analytics.com/collect?', 'params' => ['v' => 1, 'tid' => $this->trackingId, 'cid' => $uniqueId, 't' => $event->getHitType(), 'ec' => $event->getCategory(), 'ea' => $event->getAction(), 'el' => $event->getLabel(), 'cs' => $campaign->getSource(), 'cm' => $campaign->getMedium(), 'cn' => $campaign->getName(), $metricName => $metricValue]]; $url = isset($params['url']) ? $params['url'] : $defaults['url']; $url = rtrim($url, '?') . '?'; if (isset($params['url'])) { unset($params['url']); } $params = array_merge($defaults['params'], $params); $queryParams = []; foreach ($params as $key => $value) { if (!empty($value)) { $queryParams[] = sprintf('%s=%s', $key, $value); } } return $url . implode('&', $queryParams); }
/** * returns the rendered id * * @return string */ private function renderId() { $id = $this->campaign->getId(); return empty($id) ? '' : "ga('set', 'campaignId', '{$id}');"; }