File size: 3,170 Bytes
6b51d0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const { google } = require('googleapis');
const session = require('express-session');
const path = require('path');

const app = express();
const PORT = process.env.PORT || 3000;

// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(session({
  secret: process.env.SESSION_SECRET || 'your-secret-key',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: process.env.NODE_ENV === 'production' }
}));

// Serve static files from the public directory
app.use(express.static(path.join(__dirname, 'public')));

// Google OAuth2 Client
const oauth2Client = new google.auth.OAuth2(
  process.env.GOOGLE_CLIENT_ID,
  process.env.GOOGLE_CLIENT_SECRET,
  process.env.GOOGLE_REDIRECT_URI || 'http://localhost:3000/auth/google/callback'
);

// Routes
app.get('/auth/google', (req, res) => {
  const url = oauth2Client.generateAuthUrl({
    access_type: 'offline',
    scope: [
      'https://www.googleapis.com/auth/webmasters',
      'https://www.googleapis.com/auth/analytics'
    ],
    prompt: 'consent'
  });
  res.redirect(url);
});

app.get('/auth/google/callback', async (req, res) => {
  const { code } = req.query;
  try {
    const { tokens } = await oauth2Client.getToken(code);
    oauth2Client.setCredentials(tokens);
    req.session.tokens = tokens;
    res.redirect('/');
  } catch (error) {
    console.error('Error during Google callback:', error);
    res.status(500).send('Authentication failed');
  }
});

// Search Console API Endpoints
app.post('/api/search-console/submit', async (req, res) => {
  try {
    const { urls } = req.body;
    const searchconsole = google.searchconsole({
      version: 'v1',
      auth: oauth2Client
    });

    const batchRequests = urls.map(url => ({
      url,
      type: 'URL_UPDATED'
    }));

    const response = await searchconsole.urlInspection.index.inspect({
      requestBody: {
        inspectionUrl: batchRequests[0].url,
        siteUrl: req.session.siteUrl
      }
    });

    res.json({
      success: true,
      data: response.data
    });
  } catch (error) {
    console.error('Search Console error:', error);
    res.status(500).json({ error: 'Failed to submit URLs' });
  }
});

// Analytics API Endpoints
app.post('/api/analytics/submit', async (req, res) => {
  try {
    const { events, trackingId } = req.body;
    const analytics = google.analytics('v3');
    
    const batchRequests = events.map(event => ({
      v: '1',
      tid: trackingId,
      cid: req.session.clientId || '555',
      t: event.type,
      ec: event.category,
      ea: event.action,
      el: event.label,
      ev: event.value
    }));

    // In a real app, you would send these to Google Analytics
    // This is a simulation
    res.json({
      success: true,
      submitted: batchRequests.length
    });
  } catch (error) {
    console.error('Analytics error:', error);
    res.status(500).json({ error: 'Failed to submit events' });
  }
});

// Start server
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});