Skip to content

Commit

Permalink
Added gtag library
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jun 30, 2020
1 parent 91ec62d commit bc16ce6
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Tag/Gtag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct(string $template, array $parameters = [])
{
parent::__construct($template);

$this->setName('setono_tag_bag_gtag');
$this->parameters = $parameters;
}

Expand Down
1 change: 1 addition & 0 deletions src/Tag/GtagConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct(string $target, array $parameters = [])
{
parent::__construct('@SetonoTagBagGtag/config', $parameters);

$this->setName('setono_tag_bag_gtag_config');
$this->target = $target;
}

Expand Down
1 change: 1 addition & 0 deletions src/Tag/GtagEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct(string $event, array $parameters = [])
{
parent::__construct('@SetonoTagBagGtag/event', $parameters);

$this->setName('setono_tag_bag_gtag_event');
$this->event = $event;
}

Expand Down
20 changes: 20 additions & 0 deletions src/Tag/GtagLibrary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Setono\TagBag\Tag;

class GtagLibrary extends PhpTemplatesTag
{
public const NAME = 'setono_tag_bag_gtag_library';

public function __construct(string $id)
{
parent::__construct('@SetonoTagBagGtag/library', [
'id' => $id,
]);

$this->setName(self::NAME);
$this->setUnique(true);
}
}
2 changes: 2 additions & 0 deletions src/Tag/GtagSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ class GtagSet extends Gtag implements GtagSetInterface
public function __construct(array $parameters)
{
parent::__construct('@SetonoTagBagGtag/set', $parameters);

$this->setName('setono_tag_bag_gtag_set');
}
}
6 changes: 6 additions & 0 deletions src/templates/SetonoTagBagGtag/library.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script async src="https://www.googletagmanager.com/gtag/js?id=<?=$id?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
</script>
58 changes: 58 additions & 0 deletions tests/Tag/GtagLibraryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Setono\TagBag\Tag;

use PHPUnit\Framework\TestCase;
use Setono\PhpTemplates\Engine\Engine;
use Setono\TagBag\Renderer\PhpTemplatesRenderer;

/**
* @covers \Setono\TagBag\Tag\GtagLibrary
*/
final class GtagLibraryTest extends TestCase
{
/**
* @test
*/
public function it_creates(): void
{
$tag = new GtagLibrary('id');
$this->assertInstanceOf(TagInterface::class, $tag);
$this->assertInstanceOf(PhpTemplatesTagInterface::class, $tag);
}

/**
* @test
*/
public function it_has_default_values(): void
{
$tag = new GtagLibrary('id');

$this->assertSame('@SetonoTagBagGtag/library', $tag->getTemplate());
}

/**
* @test
*/
public function it_renders_with_parameters(): void
{
$tag = new GtagLibrary('id');

$renderer = new PhpTemplatesRenderer(new Engine([__DIR__ . '/../../src/templates']));

$expected = <<<SCRIPT
<script async src="https://www.googletagmanager.com/gtag/js?id=id"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
</script>
SCRIPT;

$this->assertTrue($renderer->supports($tag));
$this->assertSame($expected, $renderer->render($tag));
}
}

0 comments on commit bc16ce6

Please sign in to comment.