★No Demo: Only code samples are available
Initialize SwG client swg.js
to access all SwG functions on the web. The SwG client is distributed as a JavaScript library at https://news.google.com/swg/js/v1/swg.js
. There are two methods of initializing the JavaScript library:
Read the documentation.
<script>
tag.swg.js
is initialized, the callback returns a JavaScript object subscriptions
, which contains all the SwG functionalities.<html>
<head>
<!-- 2. Include the swg client -->
<script async type="application/javascript"
src="https://news.google.com/swg/js/v1/swg.js"></script>
<!-- 3. Add a client ready callback -->
<script>
(self.SWG = self.SWG || []).push( subscriptions => {
// swg.js initializes automatically, returns swg.js functions an object.
console.log(subscriptions);
});
</script>
</head>
<body>
<p>Your article content</p>
<!-- 1. Include Structured Data Markup -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"headline": "gTech Swg Demo",
"image": "your_publisher_logo_url",
"datePublished": "2025-02-05T08:00:00+08:00",
"dateModified": "2025-02-05T09:20:00+08:00",
"author": {
"@type": "Person",
"name": "John Doe"
},
"publisher": {
"name": "GTech Demo Pub News",
"@type": "Organization",
"@id": "gtech-demo-staging.appspot.com",
"logo": {
"@type": "ImageObject",
"url": "/icons/icon-2x.png"
}
},
"description": "This example loads a carousel of offers on click.",
"isAccessibleForFree": "False",
"isPartOf": {
"@type": ["CreativeWork", "Product"],
"name" : "GTech Demo Pub News basic",
"productID": "gtech-demo-staging.appspot.com:basic"
}
}
</script>
</body>
</html>
<script>
tag.subscriptions.init(publicationId)
or a Product ID subscriptions.init(productId)
<html>
<head>
<!-- 1. Include the swg client with the subscriptions-control attribute set to "manual" -->
<script async
subscriptions-control="manual"
type="application/javascript"
src="https://news.google.com/swg/js/v1/swg.js"></script>
<!-- 2. Add a client ready callback -->
<script>
(self.SWG = self.SWG || []).push( subscriptions => {
// 3. Explicitly specify the Pay backend version to use
subscriptions.configure({paySwgVersion: '2'});
// 4. Use a Publication ID (example.com) or a Product ID (example.com:premium)
subscriptions.init(publicationOrProductId);
});
</script>
</head>
<body>
<p>Your article content</p>
</body>
</html>