Skip to content

Commit

Permalink
Merge pull request #6 from AsterAI/master
Browse files Browse the repository at this point in the history
Added UTM markups
  • Loading branch information
bigpaulie authored Oct 31, 2017
2 parents 81e253e + f4449ec commit e42b1b3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,22 @@ In order to do that you can use the "exclude" property of the widget
]);
```

#### Add utm marks
For enterprise apps you mostly need to add UTM mark for analytics.
```php
// add by default
echo Share::widget([
'addUtm' => true,
]);

// little customization
echo Share::widget([
'addUtm' => true, // necessary flag
'utmMedium' => 'social_share',
'utmCampaign' => 'viral_retention',
]);

```

### Contributions
Contributions are most welcomed, just fork modify and submit a pull request.
39 changes: 38 additions & 1 deletion src/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,25 @@ class Widget extends \yii\base\Widget {
*/
public $htmlOptions = [];

/**
* Flag to add utm marks into the target URL
* @var boolean
*/
public $addUtm = false;

/**
* The 'utm_medium' mark. It works when $addUtm == TRUE
* @var string
*/
public $utmMedium = 'social_share';

/**
* The 'utm_campaign' mark. It works when $addUtm == TRUE
* @var string
*/
public $utmCampaign= 'retention';


/**
* Supported social networks
* @var array
Expand Down Expand Up @@ -137,6 +156,15 @@ public function init() {
ShareAsset::register($this->getView());
}

protected function buildUtm($network) {
return sprintf(
'utm_source=%s&utm_medium=%s&utm_campaign=%s',
$network,
$this->utmMedium,
$this->utmCampaign
);
}

/**
* Parse the template and create the
* specific button for the selected network
Expand Down Expand Up @@ -174,13 +202,22 @@ protected function parseTemplate($network)
}

if ($button) {
$url = $this->url;
if ($this->addUtm) {
$delimitBy = '?';
if (strpos($this->url, '?')) {
$delimitBy = '&';
}
$url .= ($delimitBy . $this->buildUtm($network));
}

$button = str_replace(
[
'{text}', '{network}', '{url}',
'{title}', '{description}', '{image}'
],
[
$this->text, $network, urlencode($this->url),
$this->text, $network, urlencode($url),
urlencode($this->title), urlencode($this->description), urlencode($this->image)
],
$button
Expand Down

0 comments on commit e42b1b3

Please sign in to comment.