|
1 |
| -# [Spotify](https://www.spotify.com) Audio Tag for [Google Tag Manager](https://tagmanager.google.com) |
| 1 | +# [`Spotify`](https://www.spotify.com) Audio Tag for [Google Tag Manager](https://tagmanager.google.com) |
2 | 2 |
|
3 | 3 | ## Description
|
4 | 4 |
|
5 |
| -- **Custom HTML Tag** for [***Google Tag Manager***](https://tagmanager.google.com) which is able detect which content is being played and how far it's been played, as integer percentage values between 1 and 99; |
| 5 | +- **Custom HTML Tag** for [***Google Tag Manager***](https://tagmanager.google.com) which is able detect which content is being played and how far it's been played, as integer values between 1 and 99 in percentage and as floting point values as seconds; |
6 | 6 | - No actual human friendly information is availabe *(no **episode**, **track**, **artist**, **album**, **playlist** or **show name** will be captured even though it might be viable via **DOM Scraping**)*:
|
7 |
| -- Each content is identified by a [`Spotify URI`](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids), which consist of three parts: |
| 7 | +- Each content is identified by a [`Spotify URI`](https://developer.spotify.com/documentation/web-api/concepts/spotify-uris-ids), which consists in three parts: |
8 | 8 | - `{platform}` **:** `{content type}` **:** `{identifier}`;
|
9 | 9 | - ***Examples:***
|
10 | 10 | - `spotify` **:** `album` **:** `7Ff0Q5oeEoVKHtxJJoXyId`;
|
11 | 11 | - `spotify` **:** `artist` **:** `1on7ZQ2pvgeQF4vmIA09x5`;
|
12 | 12 | - `spotify` **:** `episode` **:** `5DVGuabuaHaC9UxQodcyvf`;
|
13 | 13 | - `spotify` **:** `playlist` **:** `0E6htK22DGE5d4jj3r6dmS`.
|
14 |
| - - Different ***tracks*** executed within a ***playlist*** are identified only by their **duration** *(no human-readable identifiers were available in the event data)*: |
15 |
| - - This allows us to count how many tracks where executed (*partially or completely*) within a grouped content (*playlist, artists or album*). |
| 14 | + - Different ***tracks*** executed within a ***playlist*** are identified only by their **duration**: |
| 15 | + - This allows us to count how many **tracks** where executed (*partially or completely*) within a grouped content (*playlist, artists or album*). |
16 | 16 | - **Show URLs** (*i.e. [`open.spotify.com/show/3bdzILat9px7eH27ziXl8K`](https://open.spotify.com/show/3bdzILat9px7eH27ziXl8K)*) redirect to the show's last episode;
|
17 |
| - - Any URI can be transformed into a [`open.spotify.com`](https://open.spotify.com) link following the **template** below: |
18 |
| - - **`URI`**: |
19 |
| - - `spotify` **:** `album` **:** `7Ff0Q5oeEoVKHtxJJoXyId`. |
20 |
| - - **`URL`**: |
21 |
| - - `https://open.spotify.com` **/** `album` **/** `7Ff0Q5oeEoVKHtxJJoXyId`. |
22 | 17 |
|
23 | 18 | ---
|
24 | 19 |
|
|
29 | 24 | - `audio_playback_resumed`;
|
30 | 25 | - `audio_complete`;
|
31 | 26 | - `audio_progress`:
|
32 |
| - - ***Detected percentages*** values are **configured** and **validated** in the `Spotify audio Custom HTML tag` (i.e. `[ 25, 50, 75 ]`). |
| 27 | + - ***Detected percentages values*** are **configured** and **validated** in the `Spotify audio Custom HTML tag` (i.e. `[ 25, 50, 75 ]`). |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Tag Parameters |
| 32 | + |
| 33 | +- `audio_content`; |
| 34 | +- `audio_current_time`; |
| 35 | +- `audio_duration`; |
| 36 | +- `audio_percent`; |
| 37 | +- `audio_provider`; |
| 38 | +- `audio_status`. |
33 | 39 |
|
34 | 40 | ---
|
35 | 41 |
|
36 | 42 | ## `JavaScript` function to convert **Spotify's** ***URI*** into ***URL***
|
37 | 43 |
|
| 44 | +- Any URI can be transformed into a [`open.spotify.com`](https://open.spotify.com) link by following the **template** below: |
| 45 | + - **`URI`**: |
| 46 | + - `spotify` **:** `album` **:** `7Ff0Q5oeEoVKHtxJJoXyId`. |
| 47 | + - **`URL`**: |
| 48 | + - `https://open.spotify.com` **/** `album` **/** `7Ff0Q5oeEoVKHtxJJoXyId`. |
| 49 | + |
38 | 50 | ```javascript
|
39 | 51 | (function getSpotifyUrlfromUri(uri) {
|
40 |
| - var reSpotifyURI = /spotify:(album|arist|episode|playlist|show|track):([^:\/?#&]+)/i; |
| 52 | + var reSpotifyUri = /spotify:(album|arist|episode|playlist|show|track):([^:\/?#&]+)/i; |
41 | 53 | var result = { spotify_uri: uri, spotify_url: '' };
|
42 |
| - if (reSpotifyURI.test(uri)) { |
43 |
| - var matches = reSpotifyURI.exec(uri); |
| 54 | + if (reSpotifyUri.test(uri)) { |
| 55 | + var matches = reSpotifyUri.exec(uri); |
44 | 56 | var contentType = matches[1];
|
45 | 57 | var contentId = matches[2];
|
46 | 58 | result.spotify_url = `https://open.spotify.com/${contentType}/${contentId}`;
|
|
51 | 63 | );
|
52 | 64 | ```
|
53 | 65 |
|
54 |
| -### Output |
| 66 | +### Expected Output |
55 | 67 |
|
56 | 68 | ```javascript
|
57 | 69 | {
|
58 |
| - spotify_uri: 'spotify:album:7Ff0Q5oeEoVKHtxJJoXyId', |
59 |
| - spotify_url: 'https://open.spotify.com/album/7Ff0Q5oeEoVKHtxJJoXyId' |
| 70 | + spotify_uri: 'spotify:album:7Ff0Q5oeEoVKHtxJJoXyId', |
| 71 | + spotify_url: 'https://open.spotify.com/album/7Ff0Q5oeEoVKHtxJJoXyId' |
60 | 72 | }
|
61 | 73 | ```
|
62 | 74 |
|
63 | 75 | ---
|
64 | 76 |
|
65 |
| -## Importing **Tags**, **Triggers** and **Variables** |
| 77 | +## Pre-Configuration: Spotify's IFrame |
| 78 | + |
| 79 | +```html |
| 80 | +<iframe src="https://open.spotify.com/embed/track/3qN5qMTKyEEmiTZD38CmPA" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe> |
| 81 | +<script> |
| 82 | +// Send a message to the Spotify player to let it know our domain |
| 83 | +var spotifyPlayer = document.querySelector('iframe[src^="https://open.spotify.com"]'); |
| 84 | +spotifyPlayer.contentWindow.postMessage({ |
| 85 | + type: 'listeningOn', |
| 86 | + domain: window.location.hostname, |
| 87 | + gtmId: 'YOUR-GTM-CONTAINER-ID' |
| 88 | +}, 'https://open.spotify.com'); |
| 89 | +</script> |
| 90 | +``` |
| 91 | + |
| 92 | +--- |
| 93 | + |
| 94 | +## Importing **Tags**, **Triggers** and **Variables** in `Google Tag Manager` |
66 | 95 |
|
67 | 96 | - Save the `JSON` below to a file and **import** it in [*Google Tag Manager*](https://tagmanager.google.com):
|
68 | 97 | - [`Admin`](https://tagmanager.google.com/#/admin) > `Import Container`.
|
69 |
| -- Add a constant named `{{GA - Property ID}}` with your **[Google Analytics 4](https://analytics.google.com)'s Property ID**. |
| 98 | +- Add a constant named `{{GA4 - Property ID}}` with your **[Google Analytics 4](https://analytics.google.com)'s Property ID**. |
70 | 99 |
|
71 | 100 | ### Google Tag Manager Exported Container `JSON` file
|
72 | 101 |
|
|
0 commit comments