Skip to content

Commit 86bc8ab

Browse files
authored
Merge pull request #7929 from segmentio/analytics-js-snippet-update
Add guidance on correct Analytics.js snippet placement [LIBRARIES-1920]
2 parents d713849 + fcf6a74 commit 86bc8ab

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/_includes/content/snippet-helper.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,30 @@ analytics.page();
138138
```
139139
{% endcodeexampletab %}
140140

141-
{% endcodeexample %}
141+
{% codeexampletab HTML head with snippet %}
142+
```html
143+
<head>
144+
<title>My Page Title</title>
145+
<link rel="canonical" href="https://example.com/page" />
146+
147+
<!-- Segment snippet -->
148+
<script type="text/javascript">
149+
(function() {
150+
var analytics = window.analytics = window.analytics || [];
151+
analytics._writeKey = "YOUR_WRITE_KEY";
152+
analytics.SNIPPET_VERSION = "5.x";
153+
analytics.load = function(key) {
154+
var s = document.createElement("script");
155+
s.async = true;
156+
s.src = "https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";
157+
document.head.appendChild(s);
158+
};
159+
analytics.load("YOUR_WRITE_KEY");
160+
analytics.page();
161+
})();
162+
</script>
163+
</head>
164+
```
165+
{% endcodeexampletab %}
166+
167+
{% endcodeexample %}

src/connections/sources/catalog/libraries/website/javascript/quickstart.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ You can choose to install Segment to your site in 1 of 2 ways:
3030

3131
[b. Install Segment as an NPM package](#step-2b-install-segment-as-a-npm-package)
3232

33-
### Step 2a: Add the Segment Snippet
33+
### Step 2a: Add the Segment snippet
3434

3535
> info ""
3636
> You can find the latest version of the Segment snippet in the **Overview** tab of your JavaScript source.
3737
38-
To add the Segment snippet to your app:
39-
40-
Paste the snippet into the `<head>` tag of your site to install Segment.
38+
To add the Segment snippet to your app, paste it into the `<head>` tag of your site, **below the `<title>` tag and the canonical URL**. This ensures that Analytics.js can capture the `document.title` value.
4139

4240
{% include content/snippet-helper.md %}
4341

@@ -48,7 +46,7 @@ Replace `YOUR_WRITE_KEY` in the snippet you pasted with your Segment project's *
4846
4947
That snippet loads Analytics.js onto the page _asynchronously_, so it won't affect your page load speed. Once the snippet is running on your site, you can turn on destinations from the destinations page in your workspace and they start loading on your site automatically.
5048

51-
Note that you should remove other native third-party destination code that you might have on your site. For example, if you're using Segment to send data to Google Analytics, make sure you remove the Google Analytics snippet from your site source code to prevent sending the data twice.
49+
You should also remove other native third-party destination code that you might have on your site. For example, if you're using Segment to send data to Google Analytics, make sure you remove the Google Analytics snippet from your site source code to prevent sending the data twice.
5250

5351
> info ""
5452
> If you only want the most basic Google Analytics setup, there's no need to continue with the setup. Just toggle on Google Analytics in the Segment interface.

0 commit comments

Comments
 (0)