diff --git a/lib/Producers/MixpanelEvents.php b/lib/Producers/MixpanelEvents.php index 3a76b2e..0476e33 100644 --- a/lib/Producers/MixpanelEvents.php +++ b/lib/Producers/MixpanelEvents.php @@ -142,9 +142,22 @@ public function createAlias($original_id, $new_id) { "properties" => array("distinct_id" => $original_id, "alias" => $new_id, "token" => $this->_token) ); - $options = array_merge($this->_options, array("endpoint" => $this->_getEndpoint(), "fork" => false)); - $curlConsumer = new ConsumerStrategies_CurlConsumer($options); - $success = $curlConsumer->persist(array($msg)); + // Save the current fork/async options + $old_fork = isset($this->_options['fork']) ? $this->_options['fork'] : false; + $old_async = isset($this->_options['async']) ? $this->_options['async'] : false; + + // Override fork/async to make the new consumer synchronous + $this->_options['fork'] = false; + $this->_options['async'] = false; + + // The name is ambiguous, but this creates a new consumer with current $this->_options + $consumer = $this->_getConsumer(); + $success = $consumer->persist(array($msg)); + + // Restore the original fork/async settings + $this->_options['fork'] = $old_fork; + $this->_options['async'] = $old_async; + if (!$success) { error_log("Creating Mixpanel Alias (original id: $original_id, new id: $new_id) failed"); throw new Exception("Tried to create an alias but the call was not successful"); @@ -161,4 +174,4 @@ public function createAlias($original_id, $new_id) { function _getEndpoint() { return $this->_options['events_endpoint']; } -} \ No newline at end of file +} diff --git a/test/Producers/MixpanelEventsProducerTest.php b/test/Producers/MixpanelEventsProducerTest.php index 29e86df..d42f092 100644 --- a/test/Producers/MixpanelEventsProducerTest.php +++ b/test/Producers/MixpanelEventsProducerTest.php @@ -83,4 +83,19 @@ public function testCreateAlias() { $this->assertEquals($original_id, $msg['properties']['distinct_id']); $this->assertEquals($new_id, $msg['properties']['alias']); } + + public function testCreateAliasRespectsConsumerSetting() { + $tmp_file = __DIR__ . '/test.tmp'; + $this->assertFileNotExists($tmp_file); + + $options = array('consumer' => 'file', 'file' => $tmp_file); + $instance = new Producers_MixpanelEvents('token', $options); + + try { + $instance->createAlias(1, 2); + $this->assertStringEqualsFile($tmp_file, '[{"event":"$create_alias","properties":{"distinct_id":1,"alias":2,"token":"token"}}]' . PHP_EOL); + } finally { + unlink($tmp_file); + } + } }